Association, aggregation, composition

The three links between objects: association, aggregation and composition. Recognising each one in a statement or in Java code, setting multiplicities and navigability, and translating a link into attributes.

  • 22 cards
  • 4 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios

A few cards from the deck

  • Association: what is it?

    Show the answer

    A lasting link between two classes: objects of one know objects of the other and can send them messages.

    In code it takes the form of an attribute holding a reference to the other object. Without a kept reference there is no lasting link.

  • Which Team–Player link does this code express best?

    ```java
    class Team {
    private List<Player> players;

    Team(List<Player> players) {
    this.players = players;
    }
    }
    `

    • An aggregation: the players exist outside the team
    • A composition: the team owns its players and removes them along with itself
    • An inheritance: Team is a Player
    • A dependency: Player is only used for the length of a call
    Show the answer

    An aggregation: the players exist outside the team

    The players are created elsewhere and received by the constructor: the team gathers them without owning them, and they outlive it. A composition would create the objects itself.

  • A link whose maximum multiplicity exceeds 1 is translated in Java as a … of objects, for example List<Line>.

    Show the answer

    A link whose maximum multiplicity exceeds 1 is translated in Java as a collection of objects, for example List<Line>.

    A single attribute can refer to only one object. For 0..* or 1..*, the attribute is a list or a set, created empty in the constructor.

  • Aggregation: what is it?

    Show the answer

    A whole-and-parts link where the whole gathers parts that exist independently of it and can outlive it.

    A team gathers players, but if the team is dissolved the players still exist and may join another team. The diamond is hollow.

  • A student proposes to link Course and Student with a composition. Why is that a modelling mistake?

    • A student exists without this course and takes others: the course does not own them
    • A course is too abstract to own anything
    • The direction is reversed: the student should be the one owning the courses
    • A course has several students, which a composition forbids
    Show the answer

    A student exists without this course and takes others: the course does not own them

    A composition assumes exclusive parts whose life depends on the whole. A student is shared between several courses: that is an association. The number of parts is not the issue, a composition can have 1..*.