Chapter 4. Working with Relationships

What You'll Learn in This Hour:

  • How to model the connections among classes

  • How to visualize class-subclass relationships

  • How to show dependencies among classes

In the model that finished up the last hour, you were left with a set of classes that represent the vocabulary of basketball. Although this provides the basis for further exploration of what basketball is all about, it might be apparent to you that something's missing.

That “something” is a sense of the way the classes relate to one another. If you look at that model (refer to Figure 3.15), you'll see that it doesn't show how a player relates to the ball, how players make up a team, or how a game proceeds. It's as though you've constructed a laundry list of terms, rather than a picture of an area of knowledge.

In this hour, you'll draw the connections among the classes and fill out the picture.

Associations

When classes are connected together conceptually, that connection is called an association. The initial basketball model provides some examples. Let's examine one—the association between a player and a team. You can characterize this association with the phrase “a player plays on a team.” You visualize the association as a line connecting the two classes, with the name of the association (“Plays on”) just above the line. You show how to read the relationship with a filled triangle pointing in the appropriate direction. Figure 4.1 shows how to visualize the Plays on association between the player and the team.

An association between a player and a team.

Figure 4.1. An association between a player and a team.

When one class associates with another, each one usually plays a role within that association. You can show each class's role by writing it near the line next to the class. In the association between a player and a team, if the team is professional, it's an employer and the player is an employee. Figure 4.2 shows how to represent these roles.

In an association, each class typically plays a role. You can represent those roles on the diagram.

Figure 4.2. In an association, each class typically plays a role. You can represent those roles on the diagram.

You can imagine an association that you could read in the other direction: A team employs players. You can show both associations in the same diagram, with a filled triangle indicating how to read each association, as in Figure 4.3.

Two associations between classes can appear on the same diagram.

Figure 4.3. Two associations between classes can appear on the same diagram.

Associations may be more complex than just one class connected to another. Several classes can connect to one class. If you consider guards, forwards, and centers, and their associations with the Team class, you'll have the diagram in Figure 4.4.

Several classes can associate with a particular class.

Figure 4.4. Several classes can associate with a particular class.

Constraints on Associations

Sometimes an association between two classes has to follow a rule. You indicate that rule by putting a constraint near the association line. For example, a Bank Teller serves a Customer, but each Customer is served in the order in which he or she appears in line. You capture this in the model by putting the word ordered inside curly brackets (to indicate the constraint) near the Customer class, as in Figure 4.5.

You can place a constraint on an association. In this example, the Serves association is constrained to have the Bank Teller serve the Customer in order.

Figure 4.5. You can place a constraint on an association. In this example, the Serves association is constrained to have the Bank Teller serve the Customer in order.

Another type of constraint is the Or relationship, signified by {or} on a dashed line that connects two association lines. Figure 4.6 models a high school student choosing either an academic course of study or a commercial one.

The Or relationship between two associations is a constraint.

Figure 4.6. The Or relationship between two associations is a constraint.

Association Classes

An association can have attributes and operations, just like a class. In fact, when this is the case, you have an association class. You visualize an association class the same way you show a regular class, and you use a dashed line to connect it to the association line. An association class can have associations to other classes. Figure 4.7 shows an association class for the Plays on association between a player and a team. The association class, Contract, is associated with the GeneralManager class.

An association class models an association's attributes and operations. It's connected to an association via a dashed line and can associate with another class.

Figure 4.7. An association class models an association's attributes and operations. It's connected to an association via a dashed line and can associate with another class.

Links

Just as an object is an instance of a class, an association has instances as well. If you imagine a specific player who plays for a specific team, the Plays on relationship is called a link, and you represent it as a line connecting two objects. Just as you would underline the name of an object, you underline the name of a link, as in Figure 4.8.

A link is an instance of an association. It connects objects rather than classes. In a link, you underline the name of the link, just as you underline the name of an object.

Figure 4.8. A link is an instance of an association. It connects objects rather than classes. In a link, you underline the name of the link, just as you underline the name of an object.

Multiplicity

The association drawn so far between Player and Team suggests that the two classes are in a one-to-one relationship. Common sense tells you that this isn't the case, however. A basketball team has five players (not counting substitutes). The Has association must take this into account. In the other direction, a player can play for just one team, and the Plays on association must account for that.

These specifications are examples of multiplicity—the number of objects from one class that relate with a single object in an associated class. To represent these numbers in the diagram, you place them near the appropriate class, as in Figure 4.9. (The numbers can go either above or below the association line.)

Multiplicity denotes the number of objects of one class that can relate to one object of an associated class.

Figure 4.9. Multiplicity denotes the number of objects of one class that can relate to one object of an associated class.

The multiplicity in this example is not the only type. A variety of multiplicities are possible (a multiplicity of multiplicities, so to speak). One class can relate to another in a 1-to-1, 1-to-many, 1-to-1 or more, 1-to-0 or one, 1-to-a bounded interval (for example, 1-to-5 through 10), 1-to-exactly n (as in this example), or 1-to-a set of choices (for example, one-to-nine or ten).

The UML uses an asterisk (*) to represent more and to represent many. In one context Or is represented by two dots, as in 1..* (“one or more”). In another context, Or is represented by a comma, as in 5,10 (“5 or 10”). Figure 4.10 shows how to visualize possible multiplicities. (Note that the phrase at the right of each multiplicity in Figure 4.10 isn't part of the UML. It's just a label I added to help clarify things.)

Possible multiplicities and how to represent them in the UML.

Figure 4.10. Possible multiplicities and how to represent them in the UML.

Qualified Associations

When an association's multiplicity is one-to-many, a particular challenge often arises: lookup. When an object from one class has to choose a particular object from another in order to fulfill a role in an association, the first class has to rely on a specific attribute to select the correct object. That attribute is typically an identifier, such as an ID number. For example, a hotel's reservation list has many reservations, as Figure 4.11 shows.

A reservation list and its reservations are in a one-to-many multiplicity.

Figure 4.11. A reservation list and its reservations are in a one-to-many multiplicity.

When you make a reservation at a hotel, the hotel assigns you a confirmation number. If you call with questions about the reservation, you have to supply the confirmation number, so that someone looking through the reservation list can select your reservation.

In the UML, the ID information is called a qualifier. Its symbol is a small rectangle adjoining the class that has the “one” part in the one-to-many multiplicity. Figure 4.12 shows the representation. Although the multiplicity between ReservationList and Reservation is one-to-many, the multiplicity between confirmationNumber and Reservation is one-to-one.

The UML notation for a qualifier. The idea is that when you add that little rectangle, you qualify the association.

Figure 4.12. The UML notation for a qualifier. The idea is that when you add that little rectangle, you qualify the association.

Reflexive Associations

Sometimes, a class is in an association with itself. Referred to as a reflexive association, this can happen when a class has objects that play a variety of roles. For example, a CarOccupant can be either a driver or a passenger. In the role of the driver, one CarOccupant drives zero or more additional CarOccupants who play the role of passenger. You represent this by drawing an association line from the class rectangle back to the same class rectangle, and on the association line you indicate the roles, name of the association, direction of the association, and multiplicity as before. Figure 4.13 presents this example.

In a reflexive association, you draw the line from the class to itself, and you can include the roles, association name, direction of the association, and multiplicity.

Figure 4.13. In a reflexive association, you draw the line from the class to itself, and you can include the roles, association name, direction of the association, and multiplicity.

Inheritance and Generalization

One of the hallmarks of object-orientation is that it captures one of the great commonsense aspects of day-to-day life: If you know something about a category of things, you automatically know some information you can transfer to other categories. If you know something is an appliance, you already know it has an on-off switch, a brand name, and a serial number. If you know something is an animal, you take for granted that it eats, sleeps, has a way of being born, has a way of getting from one place to another, and probably has a number of other attributes (and operations) you could list if you thought about it for a few minutes.

Object-orientation refers to this as inheritance. The UML also refers to this as generalization. One class (the child class or subclass) can inherit attributes and operations from another (the parent class or superclass). The parent class is more general than the child class.

The inheritance hierarchy doesn't have to end at two levels: A child class can be a parent class for still another child class. Mammal is a child class of Animal, and Horse is a child class of Mammal.

In the UML, you represent inheritance with a line that connects the parent class to the child class. On the part of the line that connects to the parent class, you put an open triangle that points to the parent class. This type of connection stands for the phrase is a kind of. A Mammal is a kind of Animal, and a Horse is a kind of Mammal. Figure 4.14 shows this particular inheritance hierarchy, along with some additional classes.

An inheritance hierarchy in the animal kingdom.

Figure 4.14. An inheritance hierarchy in the animal kingdom.

In the figure, note the appearance of the triangle and the lines when more than one child class inherits from a parent class. Setting the diagram up this way results in a less busy diagram than showing all the lines and triangles, but the UML doesn't prohibit putting all of them in the picture. Note also that you don't put the inherited attributes and operations in the subclass rectangles, as you've already represented them in the superclass.

Child classes add to the attributes and operations they inherit. For example, a Mammal has hair and gives milk, two attributes not found in the Animal class.

A class might have no parents, in which case it's a base class or root class. A class might have no children, in which case it's a leaf class. If a class has exactly one parent, it has single inheritance. If a class has more than one parent, it has multiple inheritance.

Discovering Inheritance

In the course of talking to a client, an analyst discovers inheritance in several ways. It's possible that the candidate classes that emerge include both parent classes and child classes. The analyst has to realize that the attributes and operations of one class are general and perhaps apply to several other classes, which may add attributes and operations of their own.

The basketball example from Hour 3, “Working with Object-Orientation,” has Player, Guard, Forward, and Center classes. The Player has attributes such as name, height, weight, runningSpeed, and verticalLeap. This class has operations such as dribble(), pass(), rebound(), and shoot(). The Guard, Forward, and Center inherit these attributes and operations, and add some of their own. The Guard might have the operations runOffense() and bringBallUpcourt(). The Center might have the operation slamDunk(). Based on the coach's comments about relative heights of the players, the analyst might want to place constraints on the heights of the individuals who play each position.

Another possibility is that the analyst notes that two or more classes have a number of attributes and operations in common. The basketball model has a GameClock, which keeps track of how much time remains in a game period, and a ShotClock, which tracks the time remaining from the instant one team takes possession of the ball until it's supposed to shoot the ball. Realizing that both track time, the analyst could formulate a Clock class with a trackTime() operation that both the GameClock and the ShotClock inherit.

Abstract Classes

In the basketball model, the two classes I just mentioned—Player and Clock—are useful because they serve as parent classes for important child classes. The child classes are important in the model because you'll ultimately want to have instances of these classes. To develop the model you'll need instances of Guard, Forward, Center, GameClock, and ShotClock.

Player and Clock, however, will not provide any instances for the model. An object from the Player class would serve no purpose, nor would an object from the Clock class.

Classes like Player and Clock, which provide no objects, are said to be abstract. You indicate an abstract class by writing its name in italics. Figure 4.15 shows the two abstract classes and their children.

Two inheritance hierarchies with abstract classes in the basketball model.

Figure 4.15. Two inheritance hierarchies with abstract classes in the basketball model.

Dependencies

In a different kind of relationship, one class uses another. This is called a dependency. The most common usage of a dependency is to show that the signature of one class's operation uses another class.

Suppose you're designing a system that displays corporate forms on-screen so employees can fill them out. The employee uses a menu to select the form to fill out. In your design, you have a System class and a Form class. Among its many operations, the System class has displayForm(f:Form). The form the system displays obviously depends on which form the user selects. The UML notation for this is a dashed line with an arrowhead pointing at the class depended on, as in Figure 4.16.

A dashed line with an arrowhead represents a dependency.

Figure 4.16. A dashed line with an arrowhead represents a dependency.

Class Diagrams and Object Diagrams

So far I've talked at length about class diagrams, but I haven't said nearly as much about object diagrams. As we end this hour on relationships, we've reached a good point to discuss how and why you visualize objects.

A class diagram gives general, definitional information—the properties of a class and its attributes, as well as other classes it associates with. An object diagram, on the other hand, gives information about specific instances of a class and how they link up at specific instants in time. (“Instants” and “instances”—that's a good way to conceptualize the purpose of an object diagram.)

Here's an example: Suppose you're looking at part of a chess game, like the chess pieces in Figure 4.17.

A portion of a chess game.

Figure 4.17. A portion of a chess game.

If you don't know anything about chess, it will be difficult for you to understand what's happening in this particular configuration of chess pieces. If you had a class diagram of chess pieces, like Figure 4.18, that diagram could help you figure out some of the general rules of chess. (The upperShape attribute is just a way of describing the physical appearance of a chess piece.)

A class diagram of some chess pieces.

Figure 4.18. A class diagram of some chess pieces.

Although this might aid your overall understanding (particularly if the diagram somehow explained knightMoveTo(), queenMoveTo(), pawnMoveTo(), and pawnCapture()), you'd still need some help comprehending the specific chess position in Figure 4.17. An object diagram provides the help. Figure 4.19 models the chess position in Figure 4.17, naming the links among those specific pieces.

An object diagram that models the chess position shown in Figure 4.17.

Figure 4.19. An object diagram that models the chess position shown in Figure 4.17.

Summary

Without relationships, a class model would be little more than a laundry list of rectangles that represent a vocabulary. Relationships show how the terms in the vocabulary connect with one another to provide a picture of the slice of the world you're modeling. The association is the fundamental conceptual connection between classes. Each class in an association plays a role, and multiplicity specifies how many objects in one class relate to one object in the associated class. Many types of multiplicities are possible. An association is represented as a line between the class rectangles with the roles and multiplicities at either end. Like a class, an association can have attributes and operations.

A class can inherit attributes and operations from another class. The inheriting class is the child of the parent class it inherits from. You discover inheritance when you find classes in your initial model that have common attributes and operations. Abstract classes are intended only as bases for inheritance and provide no objects of their own. Inheritance is represented as a line between the parent and the child with an open triangle adjoining (and pointing to) the parent.

In a dependency, one class uses another. The most common usage of a dependency is to show that a signature in the operation of one class uses another class. A dependency is depicted as a dashed line joining the two classes in the dependency, with an arrowhead adjoining (and pointing to) the depended-on class.

Class diagrams show general definitional information about classes. To model specific instances of classes at specific instants in time, use an object diagram.

Q&A

Q1:

Do you ever provide a name for an inheritance relationship, as you do for an association?

A1:

The UML doesn't stop you from naming an inheritance relationship, but usually it's not necessary.

Q2:

When I'm modeling inheritance, can I also show other kinds of relationships in the same model?

A2:

Absolutely. A model isn't constrained to showing just one kind of relationship.

Q3:

In Figure 4.18, in the ChessPiece class, you show the properties color, startingLocation, and currentLocation along with the getCaptured() operation. You don't show those attributes and that operation in the Knight, Queen, or Pawn subclasses. Those classes have those features. Why don't you show them?

A3:

The inheritance symbol—the open triangle with the solid line connector—implies that the subclasses have those attributes. That's what inheritance is all about. A child class has all the attributes and operations of the parent class.

Q4:

While we're on the subject of Figure 4.18, let me ask you this: The subclasses show values for their two attributes. I thought that was something you show in object diagrams. What's the story?

A4:

Values for attributes certainly do appear in object diagrams. Recall from Hour 3 that you have the option of showing a default value for a class's attribute.

Workshop

The quiz and the exercises are designed to firm up your knowledge of the UML in the area of relationships. Each question and exercise requires you to think about the modeling symbology you just learned and apply it to a situation. The answers to the Quiz are in Appendix A, “Quiz Answers.”

Quiz

1:

How do you represent multiplicity?

2:

How do you discover inheritance?

3:

What is an abstract class?

4:

What's the effect of a qualifier?

Exercises

1:

Take the initial basketball model from Hour 3 and add links that express the relationships you covered in this hour. If you know the game of basketball, feel free to add links that represent your knowledge.

2:

According to an old adage, “An attorney who defends himself has a fool for a client.” Create a model that reflects this piece of wisdom.

3:

Draw an inheritance hierarchy of the objects in your residence. Be sure to include any abstract classes as well as all instances.

4:

Think back to the subjects you've taken in school. Model this set of subjects as an inheritance hierarchy, again with all abstract classes and instances. Include dependencies in this model. (Weren't some courses prerequisites for others?)

5:

Imagine an association between the classes Dog and Person. Now imagine the same association between Cat and Person. Draw each association and attach an association class to each one. Use the association classes to show how these associations differ from one another.

6:

Augment the ChessPiece class in Figure 4.18 to show the constraints on the height, upperShape, and color attributes. For upperShape, you'll have to think up some clever names for the shape at the top of Bishop, Rook, and King.

7:

If you play chess and if you feel ambitious, complete Figure 4.18 to model all the chess pieces. Then create an object diagram that models the start of a chess game. Include values for all the attributes. For the location attributes, you'll have to look up the naming system for the locations on a chessboard. If you're a chess aficionado, you know that chess pieces have point values: Add that attribute to ChessPiece and the default values to the subclasses.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.88.249