The most successful people in any walks of lives may not be the most talented ones but are definately the most passionate & dedicated ones.

Thursday, February 10, 2011

ALL-IN-ONE (Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency)

These terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff. But still for some, these terms look like Latin and Greek. Just wanted to refresh these terms and explain in simpler terms.

Association
1) Association is a relationship between two objects. In other words, association defines the multiplicity between objects.
2) Different types of association between objects are:- one-to-one, one-to-many, many-to-one, many-to-many.
3) Example: A Student and a Faculty are having an association.


Aggregation
1) It is a specific case of association.
2) Aggregation is also called a “Has-a” relationship i.e. When an object ‘has-a’ another object, then you have got an aggregation between them.

Composition
1) Composition is a special case of aggregation.
2) When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Difference between aggregation and composition
1) When there is a composition between two objects, the composed object cannot exist without the other object. Composition is more restrictive. This restriction is not there in aggregation.
2) In aggregation, though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional.
3) In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.
4) Example: - Let analyze Library, Student and Books.

Relationship between library and student is aggregation because a student can exist without a library and therefore it is aggregation.

Relationship between library and book is composition because A book cannot exist without a library and therefore its a composition.

In terms of coding snippet example of difference between aggregation and composition

Aggregation Example: the object exists outside the other, is created outside, so it is passed as an argument (for example) to the constructor. Ex: People – car. The car is created in a different context and then becomes a person’s property.

****************************************************************************

// WebServer is aggregated of a HttpListener and a RequestProcessor

public class WebServer

{

private HttpListener listener;

private RequestProcessor processor;
public WebServer(HttpListener listener, RequestProcessor processor)

{

this.listener = listener;

this.processor = processor;

}

}

****************************************************************************

Composition Example: the object only exists, or only makes sense inside the other, as a part of the other. Ex: People – heart. You don’t create a heart and then passes it to a person.

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

// WebServer is an composition of HttpListener and RequestProcessor & controls their lifecycle

public class WebServer

{

private HttpListener listener;

private RequestProcessor processor;
public WebServer()

{

this.listener = new HttpListener(80);

this.processor = new RequestProcessor(“/www/root”);

}

}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Here the key difference [wrt code snippet above] to note is that :-

In Aggregation we are passing the already instantiated objects [ HttpListener and RequestProcessor ] from outside the Webserver class so they exist independently of the Webserver class though Webserver class shares “has-a” relationship with them.

In Composition we are instantiating the classes [ HttpListener and RequestProcessor with in the Webserver class so they are dependent on Webserver class. Here also Webserver class shares “has-a” relationship with them.


Abstraction
1) Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction.
2) It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.
3) Example: A wire frame model of a car.



Generalization
1) Generalization uses a “is-a” relationship from a specialization to the generalization class.
2) Example: (a) Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization. (b) Lets have specialization class [Triangle and Circle] and generalization class [shape] so the relationship b/w specialization to generalization class is that every Square “is-a” Shape.



Realization
1) Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class.
2) In other words, you can understand this as the relationship between the interface and the implementing class.

Dependency
Change in structure or behavior of a class affects the other related class, then there is a dependency between those two classes.

Please feel free to disagree (if you find anything wroing) on the above explanations and believe me you won't be the first one. Do spare me for any typo's above (if any). Happy-to-learn !!!


Cheers,

Vinay

1 comment: