1.5 Composition



Composition deals with a single, complex system as an organization of more numerous but simpler systems. Composition is often used to study and explain complex human organizations (the government is divided into three major branches), complex biological systems (a human being consists of a respiratory system, a circulatory system, an immune system, a nervous systems, a skeletal system, etc.), complex machines (an aircraft consists of a propulsion system, a control system, a navigation system, etc), and complex programs (an operating systems consists of a user interface, a file system, a network system, a memory management system, etc). With composition it is important to understand both the individual (simpler) systems and the relationships or collaborations among them.

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. 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 - using standardized, specialized parts to construct a wide range of interesting artifacts.

Composition can be defined as:

Composition
an organized collection of components interacting to achieve a coherent, common behavior

The "part-whole" relationship is often expressed as a "has-a" relationship. For example, in referring to the relationship between and automobile and the automobile's windshield, the statement "the autmobile has a windhsield" suggests why the the term "has-a" is used to describe part-whole compositions.

There are two forms of composition named association (also acquaintance) and aggregation (also 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 between different compositions. A soda machine is an example of aggregation. 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 printer, a modem and a processor. Each of these interacting parts are visible to the user and directly manipulatable 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 can 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 smaller elements some of which might be other associations or aggregations of parts.

Association and Aggregation

Objects may also exhibit a complex structure formed by layers of associations and aggregations. The SimpleTimer object is an aggregation of three sub-objects: the TimedCounter, the Display, and the ControlButtons. These three sub-objects form an association: the TimedCounter and ControlButtons are both connected to the Display, and the ControlButtons is also connected to the TimedCounter. Each of the three sub-objects are themselves composed of other objects through aggregation. For example, the TimedCounter has two objects encapsulated within itself: a Clock and a Counter. These two objects in TimedCounter form an association since the Clock is connected to the Counter. Similarly, the objects encapsulated in the Display and ControlButtons objects form other aggregations or associations.


Next Stop


Last Updated: June 21, 1996 / kafura@cs.vt.edu