Composition Using Association



Association is a part-whole organization in which the "whole" is exactly defined by the "parts" and the relationships among the parts. The parts of the composition maintain their identity, their external visibility, and their autonomy in the composition. The parts are often viewed as peers, collaborators or acquaintances, such terms reflecting the primacy of the parts in the part-whole composition. In some sense, the whole is the sum of its parts. This leads to the following definition of association.

Association
a composition of independently constructed and externally visible parts.

A computer workstation, depicted below, is a typical example of a real-world association. Each of the parts shown in this figure is externally visible and can be manipulated in its own right. The notion of "computer workstation" refers to the particular assembly of these part in a way that gives rise to the functionality expected of a computer workstation. The expectations of a computer workstation would not be met by an assembly of fewer parts (i.e., no monitor), extraneous parts (i.e., two keyboards), or the corrrect parts associated differently (i.e., the mouse connected to the modem).

A Real-World Association

An association among objects is created when an object contains references or pointers to other objects. An example of an association among objects is shown below. This association creates a simple one-second timer that is displayed in a graphical user interface window and which is controlled by "Start" and "Stop" buttons. The Clock object is reponsible for determing the end of each one second interval of time. At the end of each such interval the Clock object invokes an operation on the Counter object to increment its interval value. Incrementing its value causes the Counter object to send a string representation of its value to the TextBox object. The Frame object is responsible for maintaining the consistency between the TextBox string value and the characters displayed in the user interface. The Frame also displays two buttons with the labels "Start" and "Stop" through which the user is able to control when the timer is running.

An Association of Objects

One advantage of an association form of composition is that the parts may be shared between different compositions. This is easily accomplished by having the same object be connected to (pointed to) from two objects each of which is in a different composition. Using the computer workstation example mentioned above, it is possible to have a single printer shared between two different workstations. In the one second timer example, it is possible to have multiple timers displayed in the same window through a shared Frame object. A shared Frame object is shown in the figure below. The dashed line indicates the logical partition between the two distinct one-second timers that are displayed in the same (shared) user interface window (Frame).

Shared Objects in an Association

A second advantage of an association is that the parts in an association can be dynamically changed. This can be accomplished simply by having a member in the composition connect to (point to) a different object. This change is dynamic in that it can be done at run-time. Again using the computer workstation example, it is possible to change the keyboard or mouse or to change the printer connected to the system. In the one second timer example, it is possible to replace the one second clock by a faster clock allowing more accurate timings to be made.


Next Stop


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