Using Association Classes


When you model the real world you find attributes that do not seem to fit in any one class. For instance, in our rental example you have two classes, Client and CrashDummy. Further, you know that clients rent crash dummies.

Now you want to model the attribute dayOfRental. The value of that attribute is the day a particular client rents a particular dummy. Where does the attribute belong?

Well, for openers, the dayOfRental attribute does not belong in Client; the client may rent dummies on different days. You could create attributes for client called dayOfRental1, dayOfRental2, and dayOfRental3. But if you create multiple attributes, how do you know which crash dummy was rented on dayOfRental1? On the other hand, dayOfRental doesn’t belong in CrashDummy either; any given dummy can be rented on many different days, to different clients. The solution to this dilemma: Recognize that dayOfRental is an attribute of the rents association and not an attribute of a class.

If you find an attribute whose value depends on more than one class instance, you need a third class that holds that attribute. For example, the dayOfRental attribute depends on the specific instance of Client and the specific instance of CrashDummy that were linked in the rents association on that day. You would designate the needed third class—an association class—by using a dashed line to connect the new class to the association.

Figure 4-13 shows the UML notation for showing such special attributes. The figure shows your two classes—Client and CrashDummy—in the rents relationship. It then shows another class (Rents) that contains the special attribute dayOfRental.

click to expand
Figure 4-13: The Rents association class.

In UML, a dashed line means dependency; Figure 4-13 shows dependency between the Rents class and the association named rents.

Remember The name of the association class must be the same name as that of the association—because they are really two different aspects of the same association. Association classes are, however, classes in their own right—so they can have operations as well as attributes. You can even associate your association classes to other classes—but this can get complex in a hurry. Our recommendation is to keep your modeling simple and easy to read.




UML 2 for Dummies
UML 2 For Dummies
ISBN: 0764526146
EAN: 2147483647
Year: 2006
Pages: 193

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