As a constructive activity, composition refers to the assembly of interacting parts to form a whole. The part--whole relationship is a fundamental one in object-oriented programming as one of the major goals of object-oriented programming, software reuse, is accomplished, in part, by composing existing objects (the parts) in new and different ways to form new objects (the whole). Composition might be viewed as the "Lego" approach to software development because it uses standardized, specialized parts to construct a wide range of interesting artifacts.
Composition may be defined as:
The part-whole relationship is often expressed as a "has-a" relationship as for example, in the relationship between an automobile and the automobile's windshield: "the automobile has a windshield."
There are two forms of composition: association (or acquaintance) and aggregation (or containment). These two forms of composition are similar in that they are both part-whole constructions. What distinguishes aggregation from association is the visibility of the parts. In an aggregation, only the whole is visible and accessible. In association the interacting parts are externally visible and, in fact, may be shared by different compositions. A soda machine is an example of aggregation, as the machine is a whole composed of several internal parts (a cooling system, a coin acceptor, a change maker, a soda supply). These internal parts are not visible or accessible to the normal user of the soda machine. A computer workstation is an example of composition using association. The workstation consists of a keyboard, a mouse, a monitor, a modem, and a processor. Each of these interacting parts are visible to the user and can be directly manipulated by the user.
In some cases the more generic term composition will be used in favor of the more precise terms association or aggregation. This occurs when the statement applies to both forms, when the difference between the two forms is much less important than the general idea of forming a whole from parts, or when the precise form of composition may be inferred from context.
Both forms of composition are useful. Aggregation offers greater security because its structure is usually defined in advance and cannot be altered at run-time. The implementor of the aggregation is secure in the knowledge that the integrity of the aggregation and its proper functioning cannot be adversely affected by direct interference with its internal mechanisms. Association offers greater flexibility because the relationships among the visible parts can be redefined at run-time. An association can, therefore, be made to adapt to changing conditions in its execution environment by replacing one or more of its components. Interesting design decisions can revolve around which form of composition to use, balancing a need for security against a need for greater flexibility at run-time.
The two forms of composition are frequently used together and in combinations. The computer workstation was given as an example of an association among a mouse, keyboard, processor, modem, and monitor. However, as shown in the figure below, the processor of the computer workstation is itself an aggregation that consists of hidden parts including a CPU (processor chip), memory, and a disk. More detailed examination of the processor chip would show it to be an association of even smaller elements, some of which might be other associations or aggregations of parts.
Objects may also exhibit a complex structure formed by layers of associations and aggregations. The sections on associations and on aggregations will give examples of such structures.