UML Class Diagram: Association, Aggregation and Composition

UML Class Diagram: Association, Aggregation and Composition

The UML Class diagram is used to visually describe the problem domain in terms of types of object (classes) related to each other in different ways.

There are three primary inter-object relationships: association, aggregation, and composition. Using the right relationship line is important for placing implicit restrictions on the visibility and propagation of changes to the related classes, matter which play major role in reducing system complexity.

Association

The most abstract way to describe static relationship between classes is using the ‘Association’ link, which simply states that there is some kind of a link or a dependency between two classes or more.

image

Weak Association

ClassA may be linked to ClassB in order to show that one of its methods includes parameter of ClassB instance, or returns instance of ClassB.

image

Strong Association

ClassA may also be linked to ClassB in order to show that it holds reference to ClassB instance.

image

Aggregation (Shared Association)

In cases where there’s a part-of relationship between ClassA (whole) and ClassB (part), we can be more specific and use the aggregation link instead of the association link, taking special notice that ClassB can also be aggregated by other classes in the application (therefore aggregation is also known as shared association).

image

So basically, the aggregation link doesn’t state in any way that ClassA owns ClassB nor that there is a parent-child relationship (when parent deleted all its child’s are being deleted as a result) between the two. Actually, quite the opposite! The aggregation link usually used to stress the point that ClassA is not the exclusive container of ClassB, as in fact ClassB has another container.

image

Aggregation v.s. Association

The association link can replace the aggregation link in every situation, while aggregation cannot replace association in situations were there is only a ‘weak link’ between the classes, i.e. ClassA has method/s that contain parameter of ClassB but ClassA doesn’t hold reference to ClassB instance.

Martin Fowler suggest that the aggregation link should not be used at all because it has no added value and it disturb consistency, Quoting  Jim Rumbaugh “Think of it as a modeling placebo”.

Composition (Not-Shared Association)

In cases where in addition to the part-of relationship between ClassA and ClassB – there’s a strong life cycle dependency between the two, meaning that when ClassA is deleted then ClassB is also deleted as a result, we should be more specific and use the composition link instead of the aggregation link or the association link.

image

The composition link shows that a class (container, whole) has exclusive ownership over other class/s (parts), meaning that the container object and its parts constitute a parent-child/s relationship.

Unlike association and aggregation, in the composition relationship, the composed class cannot appear as a return type or parameter type of the composite class,  thus changes in the composed class cannot be propagated to the rest of the system. Consequently, usage of composition limits complexity growth as the system grows.

——————————————————————————–

  • Composition is an Association
  • Aggregation is an Association
  • Composition is a strong Association (If the life of contained object totally depends on the container object, it is called strong association)
  • Aggregation is a weak Association (If the life of contained object doesn’t depends on the container object, it is called weak association)

Example:

class Contained {
    public void disp() {
        System.out.println("disp() of Contained A");
    }
}

public class Container {
    private Contained c;

    //Composition
    Container() {
        c = new Contained(); 
    }

    //Association 
    public Contained getC() {
        return c;
    }

    public void setC(Contained c) {
        this.c = c;
    }     

    public static void main(String[] args) {
        Container container = new Container();
        Contained contained = new Contained();
        container.setC(contained);
    } 
}

 

 

  • Simple rules:

    1. A “owns” B = Composition : B has no meaning or purpose in the system without A
    2. A “uses” B = Aggregation : B exists independently (conceptually) from A

    Example 1:

    A Company is an aggregation of People. A Company is a composition of Accounts. When a Company ceases to do business its Accounts cease to exist but its People continue to exist.

    Example 2: (very simplified)

    A Text Editor owns a Buffer (composition). A Text Editor uses a File (aggregation). When the Text Editor is closed, the Buffer is destroyed but the File itself is not destroyed.

     

     

    Difference between Association, Aggregation and Composition

    Association vs Aggregation vs Composition

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s