CSC 335 ObjectOriented Programming and Design - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CSC 335 ObjectOriented Programming and Design

Description:

2 A Car could be given the name of the gas station as a parameter ... better reuse--could use the card reader at a door without taking along the display screen ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 19
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: CSC 335 ObjectOriented Programming and Design


1
CSC 335 Object-Oriented Programming and Design
  • A Few More Design Considerations

2
Some Object-Oriented Design Guidelines
  • Uses Relationships between objects
  • Expert
  • Low Coupling
  • High Cohesion
  • Polymorphism
  • Dont Talk to Strangers

3
Relationship Between Objects
  • There are relationships between objects
  • uses relationship
  • containment relationship
  • There are relationships between classes
  • inheritance relationship
  • Let's consider how objects use other objects

4
Uses Relationship
  • A uses relationship occurs when one object sends
    another a message. Example
  • A Person object sends a setAlarm message to an
    AlarmClock object
  • public void prepareToSleep( )
  • Time wakeupTime new Time(7, 15)
  • theAlarmClock.setAlarm(wakeupTime)

5
Implementing the Uses Relationship
  • The sender must know the receiver's name
  • An object's instance variable is first and
    foremost a uses relationship
  • Every object "knows" the receiver's name
  • a JukeboxAccountCollection has a Vector of
    BankAccounts
  • The collection can send the messages to the Vector

6
Cars and Gas Stations
  • 1 If a Car "contains" a gas station instance
    variable
  • send the give_me_gas message to its gas station
  • If a Car does not "contain" a gas station
  • how can a Car send a give_me_gas message?
  • 2 A Car could be given the name of the gas
    station as a parameter
  • // aDispatcher object sends this message
  • carj.getGasoline(Exxon126)

7
Other ways to implement the uses relationship
  • 3 Let the car find a gas station on its own
  • Assume map is a global variable everyone knows
  • myGasStation map.gasStationLocation()
  • myGasStation.give_me_gasoline(10.00)
  • 4 Whenever a car needs gas, let the car build it
  • It will know the name of the object it creates
  • The gas station is destroyed upon exit

8
Referential attribute (not nice)
  • 5 Tell the car who its gas station is when the
    car is built (referential attribute)
  • provides access to an entire object that was
    constructed elsewhere

9
The Uses Relationship
  • The uses relationship is typical among the top
    level objects in an application
  • your Collection contains a Vector
  • Pass PlayerList as an argument
  • Have a global object like System.out
  • Have a method construct objects
  • Store a referential attribute
  • Get a reference to an object through a message

10
Design GuidelineRiel's Heuristic 4.1
  • Minimize the number of objects with which another
    collaborates
  • Worst case design has a collection of simple
    classes where all use each other.
  • Could one class wrap several smaller classes?
  • Example on next slide

11
A Meal class that wraps up three smaller classes
Melon
cost()
Restaurant Patron
cost()
Steak
Meal
Pie
cost()
Melon
cost()
Restaurant Patron
cost()
cost()
Steak
Pie
cost()
12
Meal Wrapper Class
  • Now it looks like there are 4 messages instead of
    3
  • encapsulation makes it look look 1 however
  • Containment simplifies things
  • Restaurant Patron collaborates with one object
    now, instead of three

13
Design Guideline
  • Containment implies a uses relationship, so
    messages should be sent to contained objects
  • If no messages are sent to a contained object, it
    doesn't belong. Put it in the right class
  • Exception Container classes
  • List, Vector, HashTables, etc. do not send
    messages to the objects
  • their job is adding, finding, and removing
    elements

14
Riel's Heuristic 4.6 and 4.7
  • Most of the methods in a class should be using
    most of the instance variables most of the time.
  • If not, perhaps we should have two classes
  • Classes should not contain more objects than a
    developer can fit in his/her short term memory
  • 7 plus or minus 2 (5 to 9 instance variables max)

15
Heuristic 4.13
  • A class must know what it contains, but the
    con-tained class should not know who contains it.
  • It's okay for a bedroom too know that it has a
    clock, but the clock shouldn't be dependent on
    the bedroom
  • an account should not send messages to DB
  • We can reuse account in other places

16
Heuristic 4.14
  • Objects that share lexical scope--those contained
    in the same containment--should not have uses
    relationships between them.
  • Consider an ATM which contains a card reader and
    a display screen
  • The card reader should not send a message to the
    display screen
  • better reuse--could use the card reader at a door
    without taking along the display screen

17
Violation of 4.14 also increases
complexity
  • An ATM may contain
  • card reader, display screen, and keypad
  • The ATM should coordinate activities between
    these three objects
  • We dont need to have additional collaborations

Automated Teller Machine
HaveACard?()
displayPIN()
Display
Card Reader
getPIN()
Keypad
18
4.11 Containment by Value Versus Containment by
Reference
  • Containment by value
  • one object contains another object
  • the contained object dies when the container dies
  • Containment by reference allows for
  • sharing of an object by a group of objects
Write a Comment
User Comments (0)
About PowerShow.com