Friday, July 29, 2011

What are association, aggregation and composition in OOPs?

Association

Association is a relationship where all object have their own life cycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own life cycle. Both can create and delete independently.
Points:
  • Is a Relationship between objects.
  • Objects have independent life cycles.
  • There is no owner.
  • Objects can create and delete independently.

Aggregation

Aggregation is a specialize form of Association where all object have their own life cycle but there is ownership and child object cannot belongs to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Points:
  • Specialize form of Association.
  • has-a relationship between objects
  • Object have independent life-cycles
  • Parent-Child relationship

On the other hand aggregation relation just represents reference between classes. For example consider Employee and contact information. Blow diagram represents the UML



The above UML describes Aggregation relation which mentions that Employee refers to Address. And life time of Address is not managed by Employee. It is like "Employee has a Address". Let us see how it can be implemented in C#.

public class Address
{
 . . .
}

public class Employee

{

     private Address address;

     public Employee(Address address)

     {

         this.address = address;

     }

     . . .

}

Composition

Composition is again specialize form of Aggregation. It is a strong type of Aggregation. Child object does not have their life cycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option cannot belong to multiple questions. If we delete questions options will automatically delete.
Points:
  • Specialize form of Aggregation.
  • Strong Type of Aggregation.
  • Parent-Child relationship.
  • Only parent object has independent life-cycle.

Lets take a simple example of Cycle. Cycle should contain 2 tyres and if we convert it to logical model below is the UML diagram




The above UML describes that Cycle contains 2 Tyres and the life time of each Tyre is maintained by Cycle. It is like "Tyre is Part Of Cycle". Let us see how it can be implemented in C#.

public class Tyre
{
 . . .
}

public class Cycle

{

    Tyre[] tires = new Tyre[]{new Tyre(), new Tyre()};

    .......

}

Have a grate day.

No comments:

Post a Comment