You might find it difficult to decide between modeling a relationship as an association, an aggregation, or a composition. Here are a few clues to look for when you’re modeling relationships:
If you hear words like “part of,” “contains,” or “owns,” then you probably have an aggregation relationship.
If the life-cycle of the
If the parts are shared, then it’s an aggregation .
If the parts are not shared, then you may have composition .
Aggregations (and composition) also have two other identifying properties: they’re not symmetric but they are transitive. Hang on, these are fancy terms for a couple of simple ideas. An association is
symmetric
if it is the same thing in both directions. Think of the relationship between a generic report and one of its
Here’s a down-to-earth way to say that again: If a filament (A) is a part of a light bulb (B), and a light bulb (B) is part of a lamp (C), then the filament (A) is also part of the lamp (C). If you can apply the transitive property, then
Table 5-1 summarizes these criteria to help you decide whether you have an aggregation, composition, or association.
|
Decision Result |
Criteria |
|---|---|
|
Aggregation or Composition |
Part-of, contains, owns words are used to describe relationship between two classes |
|
Aggregation or Composition |
No symmetry |
|
Aggregation or Composition |
Transitivity among parts |
|
Composition |
Parts are not shared |
|
Composition |
Multiplicity of the whole is 1 or 0..1 |
|
Aggregation |
Parts may be shared |
|
Aggregation |
Multiplicity of the whole may be larger than 1 |
|
Association |
Relationship does not fit the other criteria |
UML allows you to place a class diagram inside a class. When we’re talking about composites, this isn’t as
UML2
UML 2 has a new diagram
Remember The UML notation for class has three major compartments:
The first compartment
The second compartment shows the structure of the class as a list of attributes.
The third compartment is where you place the class’s behavior specification.
This compartmentalization was allowed as an interesting idea in the previous version of UML 1.4. Most of the CASE tools, however, didn’t pick up on this idea. But that is changing with UML 2.0.
Modeling the strong form of aggregation—composition—often results in a class diagram with lots of confusing lines. You have lines between the class playing the role of the whole and classes playing the role of the parts. You also have lines showing the associations between individual parts internal to the composite. With all these lines, the diagram can be difficult to read. UML 2 allows you to model composites and their parts as a class diagram within a class (composite structural diagram). This
You can show the parts of a composite inside the structure compartment of a class by
Figure 5-3:
Composite parts shown inside a class.
Parts can also be connected by (you guessed it)
connectors
—lines that
Figure 5-3 illustrates just such a diagram. The GenericReport class is playing the role of the whole or composite. The parts are anonymous parts with classes named Header, Column and Footer . One of the parts is named detail which is of the class Body . The parts are connected using lines that can be named just like associations. Indeed you can place multiplicity, role names, and qualifiers on these connections. Each of the connections shown in Figure 5-3 are assembly connections. For instance the Header will invoke the print service of Column .
This section ties together composites, part diagrams (those class diagrams inside of a class), and attributes. Figure 5-4 shows the class for GenericReport and its attributes. Notice the correspondence between the attributes in Figure 5-4 and the classes in Figure 5-3. The class definition in Figure 5-4 hides the internal structure of the GenericReport class by simply listing the major parts as attributes. The sqlStatement is not a part—rather, it’s one of the attributes of the GenericReport class.
Figure 5-4:
Showing composite parts as attributes inside a class.
If you want to convert a simple class into a composite structure diagram, you can use Table 5-2 as a guide. The table shows the correspondence between attributes in a simple class diagram and the elements of a part diagram inside of a composite class. For instance the detail attribute of the GenericReport becomes a part with the same name in the composite structure diagram. The Body datatype becomes the name of the detail part’s class. The [1..*] multiplicity is carried forward to the multiplicity of the detail part.
|
Attribute Feature |
Composite Structure Feature |
|---|---|
|
Attribute name |
Part name |
|
Type |
Part’s class name |
|
Multiplicity |
Allowable number of connections between part instances |