Sunday, July 7, 2013

Differences between Aggregation and Composition

UML diagram in Software development shows complete system by using its notation. Those are classes, properties, operation and relationships (Instance and Class Level). The most interesting notations in UML are relationship. Relationship is link between entities. There is a small extent between Aggregation and Composition in instance level relationship.

Association: It lines between two entities.

Aggregation: It shows “Has a” relationship. It is more specific than association. It is used when one part is container of other side. But contains classes do not have a strong dependency on the container. Means when container is disposed then contents are not destroyed.

Notation- in UML diagram aggregation is represented by hollow diamond shape.


Example - A Student and a Course are having an Aggregation relationship. 

    public class Student
    {
        private Course course;
        public Student(Course c)
        {
            this.course = c;
        }
    }
    public class Course
    {


    }

Composition: It shows “Owns a” relationship. It is even more specific than aggregation. It gives a high dependency on container. Where container is being disposed then contains classes must be destroyed.

Notation- in UML diagram Composition is represented by filled diamond shape.


Example - A Car and an Engine are having a Composition relationship. 

      public class Car
    {
        private Engine engine;
        public Car()
        {
            engine = new Engine();
        }
    }

    public class Engine
    {

    }

Now we are in differentiation in aggregation and composition- the relationship in association is object level. It shows container and part. Aggregation has soft relationship but composition is based on strong and believes to destroy its complete parts.

No comments:

Post a Comment