JavaRanch Home    
 
This page:         last edited 12 March 2010         What's Changed?         Edit

Association Vs Aggregation Vs Composition   

An Association is a channel between classes through which messages can be sent. As sending messages translates to calling methods in Java, Associations are typically (but not necessarily) implemented by references.

An Aggregation is an Association which denotes an "is part of" relationship. Unfortunately, the definition of this relationship is quite lax, so basically everyone is using his own interpretation. The only definitive (?) property is that in an instance graph, aggregations are not allowed to be circular - that is, an object can not be "a part of itself". (or) When building new classes from existing classes using aggregation, a composite object built from other constituent objects that are its parts.Java supports aggregation of objects by reference,since objects can't contain other objects explicitly.

A Composition adds a lifetime responsibility to Aggregation. In a garbage collected language like Java it basically means that the whole has the responsibility of preventing the garbage collector to prematurely collect the part - for example by holding a reference to it. (In a language like C++, where you need to explicitely destroy objects, Composition is a much more important concept.) Only one whole at a time can have a composition relationship to a part, but that relationship doesn't need to last for the whole lifetime of the objects - with other words, lifetime responsibility can be handed around.

Aggregation and Composition Guidelines :

Sometimes an object is made up of other objects. For example, an airplane is made up of a fuselage, wings, engines, landing gear, flaps, and so on. A delivery shipment contains one or more packages. A team consists of two or more employees. These are all examples of the concept of aggregation, which represents “is part of” relationships. An engine is part of a plane, a package is part of a shipment, and an employee is part of a team. Aggregation is a specialization of association, specifying a whole-part relationship between two objects. Composition is a stronger form of aggregation where the whole and parts have coincident lifetimes, and it is very common for the whole to manage the lifecycle of its parts. From a stylistic point of view, because aggregation and composition are both specializations of association the guidelines for associations apply.

Related Saloon Threads:

Other Resources:


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton