Relationships



There are several degrees of relationships that can be modeled.

Association

In an association, the state of the object depends on another object.  There are many types of association, such as owns (Tom owns the book), or works for (Jim works for Cynthia). The association affects both sides of the relationship.

In as association, the two objects have a strong connection but neither one is part of the other.  The objects participating in an association often have independent existences.

 

Aggregation And Composition

Aggregation and composition refer to relationships in which a class has references to objects of other classes.  They are a special form of association, sometimes referred to alternatively as the 'contains,' 'is composed of,' 'is comprised of,' 'is part of,' 'consists of,' or some other phrase that implies a containment relationship.  This technique allows reuse of existing code and collaboration with other implementations.


Aggregation and Composition

Both relationships are useful. 

Interesting design decisions can revolve around which form to use, balancing a need for security against a need for greater flexibility at run-time.


Some texts use the term composition generically.  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 relationship may be inferred from context.


Adapted from Object-Oriented Software Design and Construction with Java by Dennis Kafura


Shapiro addresses the concept of composition in the following excerpt.  He makes the distinction between aggregation and composition by noting that composition refers to nesting classes inside one another, while in aggregation they are two distinct classes.

Some OOP experts make a distinction between composition and aggregation; others will tell you they both mean the same thing. At the conceptual level, expressing an alternative to the is-a relationship, that may be true. But at the implementation level there are important differences, as you will shortly see.

While inheritance represents the is-a relationship between classes, aggregation and composition represent the has-a relationship between classes. A bus, for example, has seats for its passengers, but a bus is not a seat. The seats are embedded in the bus as member objects. Seats have their own class hierarchy, as do busses. The hierarchies of both classes remain separate and distinct because a bus cannot be a seat and visa versa.

Aggregation is exactly how your form classes come to life with all manner of functionality. You embed components and controls onto the form, like buttons and text boxes. Forms, buttons, and text boxes, however, are distinctly separate objects. A form is not a text box and a text box is not a button (although there may be traces of a common ancestor far back up the chain in the base classes for components and controls). 

Where aggregation differs from composition is that aggregate objects or aggregates are sub-types that are embedded in the container class and composite classes or composites are classes that are composed in (nested within) the container class. Aggregates are not exclusive to their container and may be embedded in other containers. Aggregates can be any exposed types in any assembly that can be declared and instantiated in the container class. Composites on the other hand are exclusive to their container. They are formally declared and implemented in a container class as a composite or nested class.

Additionally you always have direct access to the implementation, or source code, in a composite class. Aggregate objects are not exposed in this manner; you have no access to their implementation and source.

 

Code segment demonstrating Composition.

Public Class Order
     Private orderItems As ArrayList
     Private shippingAddress As Address

     Private Class OrderStatus
          Private isShipped As Boolean
          Private isPaid As Boolean
     End Class ' OrderStatus
     Private status As OrderStatus

End Class '
Order

Shapiro, J.R. Visual Basic.NET:The Complete Reference, McGraw-Hill/Osborne, Berkeley, CA, 2002, pp. 393-394.


See Nested Classes in Visual Basic.Net for information about implementing composition. 

 

Please Report Errors in Notes Here