Consolidating the concepts in this chapter
This chapter concentrates on the concept of linking to provide association and composition in contrast to inheritance. Association and composition delegate functionality to their associates and constituents while inheritance inherits functionality from its parents.Compared to inheritance, association and composition allow the programmer much more flexibility but at the expense of greater programming effort.
Compared to association, composition encapsulates its constituents, introducing the notion of ownership. A composed class must therefore propagate all operations that are performed on it (eg creation and destruction) to its constituents and must create deep copies so that it has private copies of existing objects. Delphi supports the creation of deep copies through the TPersistent class and its Assign and AssignTo methods.
Like inheritance, composition allows re-use. However, while inheritance exposes the ancestor class in its entirety, composition allows us to expose only those parts of another class that we need while hiding those parts we dont need. Access to the constituent class(es) is through the composed object only outsiders cannot address the constituents directly and this is how composition helps to maintain encapsulation and to make a program more robust.
Composition also allows us to re-use parts of several different classes in the same composed class to give a simpler, though less powerful, alternative to multiple inheritance. (This is why the designers of Delphi and Java, among others, could leave out multiple inheritance from their respective languages.)
Association has several related characteristics that distinguishes it from both inheritance and composition. With association, any change in the associated class is directly reflected in the associating class (eg a change in a TCustomer is directly reflected in all associating TSales). This is useful when the change is appropriate, but can lead to access errors or other errors if not. Association also allows several different objects to be associated with the same object. For example, several TSale objects can be linked to the same TCustomer using association, while with composition each TSale makes exclusive use of its own TCustomer. These various characteristics of association can be very useful, as well see in the Player Role pattern for example.
One of the major differences between composition and association is the question of ownership. A composed object owns its onstituents; it Creates them and Frees them. With association, objects do not own one another and their lifetimes are not necessarily bounded by one another. One does not (typically) Create or Free the other. It simply carries a link to the other. This also means that an association can change at runtime, by pointing the link to a different object.
Unlike inheritance, neither association nor composition is supported directly by the language, and the programmer must specifically declare and maintain the links. The associating or composed class participates fully within its own inheritance hierarchy, for example in terms of polymorphic substitution, and not in the constituent classs.
The following comparison highlight some of the important details within these concepts.
Comparing inheritance to association and composition
Inheritance- A standard part of an OO language.
- Reuse the entire implementation of the superclass.
- Results in a single class, the subclass, whose structure exposes the entire structure of the superclass.
- No constituent objects and so no propagation.
- Polymorphism within the subclasss hierarchy.
- With single inheritance, reuse of only a single hierarchy.
- Must be implemented by the programmer.
- Reuse selected parts of the associates or constituents functionality.
- Results in a set of linked classes with any exposure of the constituents under the programmers control.
- Actions on the composed class must be propagated to the constituent objects.
- Polymorphism within the composed or associating classs hierarchy and not within any of the constituents hierarchies.
- Reuse of objects from several different hierarchies if needed, giving a simplified alternative to multiple inheritance.

