0

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).

iAmLearning
  • 1,153
  • 3
  • 15
  • 28
  • http://stackoverflow.com/questions/11881552/implementation-difference-between-aggregation-and-composition-in-java – mihaisimi Dec 04 '14 at 17:44
  • hi mihaisimi ! I also went through the link but have I understood it correctly, is the example above the correct way to implement it from a class diagram ? – iAmLearning Dec 04 '14 at 18:02
  • I understand the basics and difference between aggregation and composition, but a little confused from the class diagram above and the implementation for numbers placed on the arrows for when it will be 0 or 1 – iAmLearning Dec 04 '14 at 18:06
  • Ah, I got confused then :). Well, if you want to make sure a parameter is instantiated you can always mark it final. This will force you to instantiate it either inline or in the constructor and thus it can't be 0 anymore and will satisfy the 1->1 . Eg, in classB change to "private final ClassC classC". – mihaisimi Dec 04 '14 at 18:22

3 Answers3

1

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(..........));
   }

}
mihaisimi
  • 1,911
  • 13
  • 15
0

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.

BobRodes
  • 5,990
  • 2
  • 24
  • 26
0

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.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50