Search code examples
javaumlaggregationcompositionclass-diagram

Composition and aggregation issue Java


Can someone please explain me what is type of aggregation is and why for that the vector should not be initialized and this code is according to diagram? What I don't understand why when is -> at the end of aggregation or composition the vector shouldn't be internalized.

class Project
{
 private Vector<Student> stds;
 private Vector<Diag> diagra = new Vector();
}

UML:enter image description here


Solution

  • This code does not allow to make a difference between association, aggregation, composition. The initialization or not of the vector is not relevant.

    The UML says that a Project has an unspecified number of Student. Since it is aggregation, there is no exclusive ownership, sot the same students can appear in several projects. ALsi, when the project is destroyed, all its students remain unaffected.

    • The Java code leaves stds unitialized. This says nor guarantees anything about ownership of the Student.
    • Moreover, the Java code will have at one moment or the other to properly initialize the stds vector if the project is supposed to aggregate some students.
    • In fact, I suspect the author of the code to confuse aggregation with the object assignment in Java. Aggregation is a modeling placebo that does not have this meaning. It is much simpler and less ambiguous to use a simple association instead of an agregation in this case.

    The UML says the a Project is a composite that has an unspecified number of Diag. The composition means an exclusive ownership, not of the vector, but of the diagrams that are stored within:

    • The java code uses vector that is initialized to an empty vector, which tells nothing about teh lifeclycle and ownership of the objects it will contain.
    • In fact, the vector elements are references (“object handles”) that can always be shared. Nothing guarantees the ownership of the diagrams in the code.