Page 507 - Introduction to Programming with Java: A Problem Solving Approach
P. 507
Figure 12.1 shows a composition hierarchy for a human body. At the top of this particular composition hierarchy is a whole body. A human body is composed of several organs—brain, heart, stomach, bones, muscles, and so on. Each of these organs is in turn composed of many cells. Each of these cells is composed of many organelles, like the nucleus (a cell’s “brain”), and the mitochondria, (a cell’s “muscles”). Each or- ganelle is composed of many molecules. And finally, each organic molecule is typically composed of many atoms.
In a composition hierarchy (as well as in an aggregation hierarchy), the relationship between a contain- ing class and one of its part classes is known as a has-a relationship. For example, each human body has a brain and has a heart. Remember that with a composition relationship, a component part is limited to just one owner at a time. For example, a heart can be in only one body at a time. Although the ownership is ex- clusive, it’s possible for the ownership to change. With a heart transplant, a heart can switch to a new owner, but it still has just one owner at a time.
Note the diamonds in Figure 12.1. In the Universal Modeling Language (UML), solid diamonds denote a composition relationship. They indicate that a whole has exclusive ownership of a part.
Now let’s think about an aggregation example where the parts are not exclusively owned by the whole. You can implement a school as an aggregation by creating a whole class for the school and part classes for the different types of people who work and study at the school. The people aren’t exclusively owned by the school because a person can be part of more than one aggregation. For example, a person can attend classes at two different schools and be part of two school aggregations. The same person might even be part of a third aggregation, of a different type, like a household aggregation.
12.2 Composition and Aggregation 473
Apago PDF Enhancer
human body
muscles
cells
nucleus
mitochondria
molecules
atoms
brain heart stomach bones
Figure 12.1 Partial representation of a composition hierarchy for a human body