By Rick Mercer with help from - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

By Rick Mercer with help from

Description:

4 Whenever a car needs gas, let the car build it. It will know the name of the object it creates ... The car has a reference needed by someone else. Be careful: ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 7
Provided by: rickmercer
Category:
Tags: help | mercer | rick

less

Transcript and Presenter's Notes

Title: By Rick Mercer with help from


1
The Relationships Among Objects
  • By Rick Mercer with help from
  • Object-Oriented Design Heuristics
  • Arthur Riel
  • Addison-Wesley, 1996, ISBN 0-201-6338-X

2
Relationship Between Objects
  • Object relationships occur when one object
  • sends a message to an instance variable
  • is passed as an argument in a message
  • creates another object
  • reference is returned from a message
  • stores a referential attribute

3
Example Cars and Gas Stations
  • 1 If a Car "contains" a gas station as an
    instance variable (not intuitive)
  • A car can send a getGasoline message to its own
    gas station
  • myGasStation.getGasoline(10.00)
  • 2 If a Car does not "contain" a gas station, a
    Car could be given the name of the gas station as
    an argument
  • // aDispatcher object sends this message
  • aCar.getGasoline(aGasStation)

4
Other uses relationships
  • 3 Let the car find a gas station on its own
  • Assume map is a global variable everyone knows
  • GasStation aGasStation
  • map.getNearestGasStation()
  • aGasStation.getGasoline()
  • 4 Whenever a car needs gas, let the car build it
  • It will know the name of the object it creates
  • The gas station will be destroyed upon exit

5
Who constructs needed objects?
  • What object creates object X? ignoring
    special-cases designs such as Factory
  • Choose an object B to create an X such that
  • B contains X
  • B closely uses X
  • B has the initializing data for X

6
Referential attribute (can be bad)
  • 6 Store a reference to a gas station that was
    created elsewhere
  • Known to some as a referential attribute
  • This provides access to an entire object that was
    constructed elsewhere
  • The car has a reference needed by someone else
  • Be careful This can lead to incorrect behavior
  • Would be better to have just one instance with a
    global point of reference use Singleton to be
    discussed later
Write a Comment
User Comments (0)
About PowerShow.com