Aggregation poses a problem when interpreting some of the related associations. The composition context helps to assist with the problem of interpreting some of the related associations.
Take a look at the Book class diagram below. When we first showed this example to you, we used aggregation. Realistically, it would be better modeled as composition, because the chapters and other parts cannot exist apart from the book. Now let us add some additional associations to show where this notation breaks down.
Looking at the Book class diagram, can you determine whether or not a footnote can refer to chapters in the same book or only chapters in other books, or if footnotes may refer to the book in which they reside or only to other books?
The context notation provides a very precise specification that handles these limitations of the current diagram. Start with a large rectangle. The name of the composite class becomes the name of the context. Inside the context, place the component class or classes.
The nesting continues for all levels of the composition. Place the multiplicity of the parts in the top-right corner of the context box. The name of the part may be just the class name or it may include the role that the part plays in the composition, using the notation role:classname.
Step through the creation of the book composition context to see how the parts fit together.
Start with the Book context. Draw the box and place the name at the top.
Place the Chapter context inside of the Book context to show containment. Name the new component context and place the multiplicity in the top right corner.
Place the Footnote context inside the Chapter context to show containment.
A footnote may refer to another Chapter in the same book. Draw the association line from the edge of the Footnote out of the Chapter context and back to the outside edge
Footnotes can also refer to Chapters in other Books. Draw the association line from the outside edge of the Footnote, out of the Chapter and the Book context,
A Footnote can refer to the Book in which it resides. Draw the association line from the Footnote to inside edge of the Book context.
The context diagram is a general-purpose tool for presenting your models. Composition is only one possible use. Use the context diagram for any presentation where you want to limit the scope of the elements under review.
In the next lesson, you will learn the notation for specifying inheritance.