iAmLearning
iAmLearning

Reputation: 1174

Aggregation and Composition Implementation in java using Class Diagram

I am trying to understand the aggregation and Composition.
Suppose I have something like below:
enter image description here

and I want to implement it using java, is the below implementation correct ?

public class ClassC { private String z; }  

public class ClassB { 
   private String y; 
   private ClassC classC;
   //-----setter and getter for classC
}   


public class ClassA {
   private String x;
   private List<ClassB> classBList;
   public ClassA(final List<ClassB> classBList) {
      this.classBList=classBList
   }
}  

Also, how to ensure that ClassB can have exactly 1 ClassC ?
and ClassA can have 1 or more ClassB ? as marked on the arrows(if I understand these notations correctly).

Upvotes: 0

Views: 3344

Answers (3)

Geert Bellekens
Geert Bellekens

Reputation: 13701

There are many correct ways to implement such a class diagram.

But in order to choose the correct implementation you should first make sure you understand the concepts of Association, Aggregation and Composition as they are defined in UML.

I've written an article about that on my website: UML Composition vs Aggregation vs Association

In short, the Composition is a type of Association with real constraints and impact on development, whereas the Aggregation is purely a functional indication of the nature of the Association with no technical impact.

Upvotes: 0

BobRodes
BobRodes

Reputation: 6165

You can model it this way, using your class diagram as an example. For ClassA, do as mihaisimi does: instantiate a list of ClassB in ClassA's constructor. If you want to determine exactly how many ClassB instances should be a part of a given ClassA instance, just add an integer parameter to the constructor and throw an exception if it's a 0. Then use the value passed as a loop counter for the Add method.

For ClassB, add a parameter to its constructor of type ClassC. So, something like this (java's a bit rusty, so feel free to correct my syntax as needed):

public class ClassA
{
    public final List<ClassB> classBList;
    public ClassA(int y){
        this.myClassBList = x;
        classBList = new ArrayList<ClassB>(y);
        for(int i=0;i<y;i++)
        {
             classBList.add(new ClassB(new ClassC));
        }
}

public class ClassB
{
    public ClassC myClassCInstance; 
    public ClassB(ClassC myClassC)
    {
        myClassCInstance = myClassC;
    }
}

As you can see, this allows you to have any number of ClassB instances associated with a ClassA instance, and ties their lifetime to ClassA as composition requires. Also, as you can see, you can't add more than one ClassC instance to any instance of ClassB, and ClassB's lifetime isn't strictly coupled to ClassB's.

So the basic difference in how to model composition and construction is this: for composition, instantiate the composed object in the composing object's (the one with the diamond) constructor, and for aggregation, pass the aggregated object into the aggregating object's constructor as a parameter.

Upvotes: 0

mihaisimi
mihaisimi

Reputation: 1999

I think what might be confusing is the difference between composition and aggregation as both are a sample of "has a" relation. However, composition is stronger than aggregation, the containing object controls the entire lifecycle of the part object.

You could write it with final as you did but it doesn't quite hit the mark:

public class ClassA {
   private String x;
   private final List<ClassB> classBList;
   public ClassA(String x, List<ClassB> classBList) {
      this.classBList=classBList;
      this.x = x;
   }
}  

I think this would make for a clearer representation:

public class ClassA{
    private String x;
    private final List<ClassB> classBList;
    public ClassA(String x){
       this.x = x;
       classBList = new ArrayList<ClassB>(2);
       classBList.add(new ClassB(..........));
       classBList.add(new ClassB(..........));
   }

}

Upvotes: 1

Related Questions