Expressing Aggregation In A UML Class Diagram


Aggregation

An aggregation is an association between two objects that results in a whole/part relationship. An object comprised of other objects (a.k.a. the whole object) is referred to as an aggregate. Objects used to build the whole object are called part objects. There are two types of aggregation: simple and composite. Each type of aggregation is discussed in detail below.

Simple vs. Composite Aggregation

Aggregate objects can be built from one or more part objects. An aggregate object can be a simple aggregate, a composite aggregate, or a combination of both. The difference between simple and composite aggregation can be found in how the whole or aggregate object is linked to its part objects. The type of linking between an aggregate and its parts will dictate who controls the lifetime (creation and destruction) of its part objects.

The Relationship Between Aggregation and Object Lifetime

The difference between simple and composite aggregation is dictated by who (i.e., what object) controls the lifetime of the aggregate’s part objects.

Simple Aggregation

If the aggregate object simply uses its part objects and otherwise has no control over their creation or existence then the relationship between the aggregate and its parts is a simple aggregation. The part object can exist (i.e. it can be created and destroyed) outside the lifetime of the simple aggregate object. This leads to the possibility that a part object can participate, perhaps simultaneously, in more than one simple aggregation relationship.

The Java garbage collector will eventually destroy unreferenced objects, but so long as the simple aggregate object maintains a reference to its part object, the part object will be maintained in memory.

References to existing part objects can be passed to aggregate object constructors or other aggregate object methods as dictated by program design. This type of aggregation is also called Containment By Reference.

Composite Aggregation

If the aggregate object controls the lifetime of its part objects (i.e., it creates and destroys them) then it is a composite aggregate. Composite aggregates have complete control over the existence of their part objects. This means that a composite aggregate’s part objects do not come into existence until the aggregate object is created.

Composite aggregate part objects are created when the aggregate object is created. Aggregate part creation usually takes place in the aggregate object constructor. However, in Java, there is no direct way for a programmer to destroy an object. You must rely on the JVM garbage collector to collect unreferenced objects. Therefore, during an aggregate object’s lifetime it must guard against violating data encapsulation by not returning a reference to its part objects. Strict enforcement of this rule will largely be dictated by program design objectives.

This type of aggregation is also called Containment By Value.

Quick Review

An aggregation is an association between two objects that results in a whole/part relationship. There are two types of aggregation: simple and composite. The type of aggregation is determined by the extent to which the whole object controls the lifetime of its part objects. If the whole object simply uses the services of a part object but does not control its lifetime then it’s a simple aggregation. On the other hand, if the whole object creates and controls the lifetime of its part objects then it is a composite aggregate.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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