Friday, May 23, 2008

Association, Aggregation ,Composition, Specialization & Generalization

Association is a relationship where all object have their own lifecycle 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 lifecycle. Both can create and delete independently.

Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object. Let’s take an example of Department and Professor. A single professor can belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.

Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle 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 can not 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 can not belong to multiple questions. If we delete questions options will automatically delete.

For example, a university owns various departments (e.g., I.T.), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors . In addition, a professors could work in more than one department, but a department could not be part of more than one university.

Composition is usually implemented such that an object contains another object. For example, in C#:

class Professor
{
}
 // If department is deleted professor still exists.
// professor can work more than one department.
class Department // Aggregation
{
private List<professor> professorList = new List<professor>();
}
 // If university is deleted professor department no longer exists.
//department can not be part of more than one university.
class University // Composition
{
           private List<Department> DepartmentList = new List<Department>();
}

Specialization & Generalization

Classes and their instances (objects) do not exist in a vacuum, but rather in a network of interdependencies and relationships, just as we, as social animals, live in a world of relationships and categories.

One of the common relationship is specialization (is a) relationship. For example Dog is Mammal so it has all the characteristics of mammal but reverse is not true. Means some of characteristics present in the dog are not exists in all mammals which are known as specialization. Cat is also mammal so all the characteristics of mammal are present in cat but some characteristics which are different than all mammal which makes it different from Dog which is also mammal.

The specialization and generalization relationships are both reciprocal and hierarchical. Specialization is just the other side of the generalization coin: Mammal generalizes what is common between dogs and cats, and dogs and cats specialize mammals to their own specific subtypes.

These relationships are hierarchical because they create a relationship tree, with specialized types branching off from more generalized types. As you move "up" the hierarchy, you achieve greater generalization. You move up toward Mammal to generalize that dogs, cats, and horses all bear live young. As you move "down" the hierarchy you specialize. Thus, the cat specializes Mammal in having claws (a characteristic) and purring (a behavior).

Similarly, when you say that ListBox and Button are Windows, you indicate that there are characteristics and behaviors of Windows that you expect to find in both of these types. In other words, Window generalizes the shared characteristics of both ListBox and Button, while each specializes its own particular characteristics and behaviors.


2 comments:

Anonymous said...

The example given for Aggregation is wrong.

Anonymous said...

Properly explained here

http://en.wikipedia.org/wiki/Aggregation_(object-oriented_programming)#Aggregation