4.3 Six Different Ways to Implement the Uses Relationship

 <  Free Open Study  >  

Let us consider how the uses relationship is implemented. If an object wants to send a message to another object, then the first object must know the name of the second object. How can it know its name ? Consider the automatic teller machine. Somewhere in the process of a customer withdrawing money, the ATM requires the cash dispenser to hand the customer his or her money. This implies that the ATM is not capable of carrying out all of the work necessary to withdraw money. It uses a CashDispenser object to perform some of the work (along with a lot of other objects as well: Bank , CardReader , DisplayScreen , Keypad , etc.); see Figure 4.2.

Figure 4.2. ATM using a cash dispenser for a user .

graphics/04fig02.gif

How does the ATM know the name of the CashDispenser object? In this case we will find that the actual relationship is one of containment, not uses. A jump of intuition is necessary to determine that ATMs contain as one of their data members an object of class CashDispenser . It knows the name of this data member implicitly; therefore, it can send it a message with no further work. We will see that many uses relationships are refined into containment relationships. Likewise, all containment relationships are first and foremost uses relationships. Many object-oriented gurus claim that finding uses relationships is an analysis activity while refining uses into containment constitutes a design activity; others disagree . Since we follow an iterative model of software development in the object-oriented paradigm, the line between analysis and design is blurred, leaving arguments along these lines solely for academic purposes.

If a uses relationship is not a containment relationship, then how does the first object (the sender) know the name of the second object (the receiver)? Consider the relationship between a car and a gas station. It doesn't make sense to say that a car contains a gas station; however, cars do ask gas stations to give them gasoline. How does the car know the name of the gas station (see Figure 4.3)? There are five implementations of the uses relationship aside from using a containment relationship.


Figure 4.3.

graphics/04fig03.gif


The number one implementation of the uses relationship (aside from uses via containment) is that the car is given the name of the gas station as a formal parameter of the message (see Figure 4.4). Consider a higher-level object sending the car the get_gasoline() message passing the name of a gas station as an argument ”"Car get gasoline at the gas station G, which exists at location ."

Figure 4.4. Car receiving gas station as a parameter.

graphics/04fig04.gif

Another possibility is that the car asks a third-party class (a map) for the name of an appropriate gas station (see Figure 4.5). Of course, this only postpones our problem. How do we know the name of the map object?

Figure 4.5. Car receiving gas station from a third-party class.

graphics/04fig05.gif

A third possibility is that all cars in the world go to one global gas station and we all know its name by convention (see Figure 4.6). This is actually a special case of the first method, since global data are considered implicit parameters to a method.

Figure 4.6. All cars use a global gas station.

graphics/04fig06.gif

The fourth method is for the wealthy. Whenever our car needs gasoline, we pull over onto the side of the road, buy the land that's there, build a gas station, use the gas station, and destroy the gas station when we leave. In short, the get_gasoline() method for the car class builds a gas station as a local object, uses it, and then destroys it on exiting the method (see Figure 4.7). While this is not appropriate for the car/gas station domain, there are many domains where building a local object to perform some functionality is useful.

Figure 4.7. Car building a gas station for use.

graphics/04fig07.gif

The fifth and last method for implementing the uses relationship is that "god" tells a car, when it is built, who its gas station is. The car stores this information in a special type of attribute called a referential attribute for later use in the get_gasoline method. We will discuss more about this method of implementation in Chapter 7.

 <  Free Open Study  >  


Object-Oriented Design Heuristics
Object-Oriented Design Heuristics (paperback)
ISBN: 0321774965
EAN: 2147483647
Year: 1996
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net