Class Relationships Lecture Oo 09 Association Relationships References



















- Slides: 19

Class Relationships Lecture Oo 09 Association Relationships

References n Ambler, S. , The Object Primer, Cambridge Univ. Press, 2001, Section 5. 7, 5. 8

Teaching Points n n n Association Relationships Aggregation Composition

Review n How can we get the effect of run-time binding in non-OO languages? – e. g. Who_AM_I()

Class Diagrams n n n Provide a static view of the design The types of objects in the design and the static relationship between them Three perspectives – Conceptual – Specification – Implementation

Association Relationships n Associations usually represent objects of one class making use of objects of another class

Association Relationships n Conceptual – conceptual relationship n Specification – responsibilities – methods implied n Implementation – visibility (pointers) – navigability

Roles

Multiplicity (Cardinality) – 1. . * – 0. . 1 – 0. . * only one or more zero or one zero or more (also ‘*’)

Aggregation n A kind of association used to model whole-part relationships The whole is a container or composite The things being aggregated can be called parts or components

Aggregation

Benefits of showing Aggregation n Clarifies whether the part can exist independent of the whole – impacts create/delete dependencies n n n Assists in identifying the creator Operations such as copy, move, delete, etc. should often be propagated to the parts Parts hidden within the whole can provide better encapsulation

Composition n n A kind of aggregation Stronger relationship than plain aggregation The part object may belong to only one whole The parts live and die with the whole In implementation the component is structurally part of (a field of) the whole

Composition

Aggregation vs. Association – It is not always obvious when to use association and when to use aggregation – There are differing views in the OO world – Aggregation often starts out as association, but as the semantics of the problem are refined an association can become aggregation – More important at the implementation perspective

When to use Aggregation n n The lifetime of the part is bound within the lifetime of the aggregate - there is a create delete dependency of the part on the whole. There is an obvious whole-part physical or logical assembly

When to use Aggregation n Some properties of the composite propagate to the parts, such as its location Operations applied to the composite propagate to the parts, such as destruction, movement, recording, etc. If in doubt, leave it out.

Composite Pattern (Review) n May be used to describe: • • File system Graphics objects Aircraft Components etc.

Teaching Points n n n Association Relationships Aggregation Composition