Monday, 25 April 2016

Difference between the Composition, aggregation, association Relationships.

Difference between the Composition, aggregation, association Relationships.

Now first look into the composition:


To understand the design first of all we must know the relationships. The most important relationships are

 Association
 Aggregation
 Composition

Now see the below about each one elaborately.

Association:

Association is a relationship where all objects 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. This represents the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.


Let us see the small example

|Aclass|----------->|BClass|

class Aclass
{
  private:
    BClass* itsBClass;
};

Aggregation:


Aggregation is a specialized form of Association where all objects have their own life cycle, but
there is ownership and child objects can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not be destroyed. We can think about it as a “has-a” relationship. Aggregation [...] is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).

Let us see small Example

|Node|<>-------->|Node|

class Node
{
  private:
    vector<Node*> itsNodes;
}


Composition:


Composition: is again specialized form of Aggregation and we can call this as a “death” relationship.
It is a strong type of Aggregation. Child object does not have its life cycle and if parent object is deleted, all child objects 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 belong to two different houses. If we delete the house - room will automatically be deleted. 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 be deleted. Composition [...] is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

 Here is the simple example

|ClassA|<#>-------->|ClassB|

class ClassA
{
  public:
    virtual ~ClassA () {delete itsClsB;}
  private:
   ClassB * itsClsB

};



No comments: