1.5 Composition


Composition deals with a single, complex system as an organization of numerous simpler systems and is often used to study and explain complex human organizations (the American 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 system, 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 system consists of a user interface, a file system, a network system, a memory management system, etc). With composition it is important to understand 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 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:

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 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.


Association and Aggregation

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.

 

Tasks

  1. Consider a merchandising company that accepts phone orders and relays the orders to a warehouse where the merchandise is packaged and shipped via a package-delivery service. Draw a diagram showing the associations and/or aggregations in this system.

  2. Consider a home entertainment system with a satellite dish, a television, and a remote control. Draw a diagram showing the associations and/or aggregations in this system.

  3. Consider an air traffic control system with radars, controllers, runways, and aircraft.. Draw a diagram showing the associations and/or aggregations in this system.



©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement