Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of Association,Aggregation and composition into code in java?

Tags:

java

uml

I know There are different way of reprentation for Conversion of Association,Aggregation and composition in java. But when we convert them into code(java classes) they are all represented in same manner. Like Student taught by teacher which is association will be represented with Student class having instance variable of Class Teacher.

Department has professors which is aggregation will be also be represented with Department class having instance variable (array )of Class Professors.

University has departments which is composition will be also be represented with University class having instance variable (array )of Class department.

So all are reprented in same manner in terms of code. So what benefits terms Association,Aggregation and composition provide to developer?

like image 387
M Sach Avatar asked Aug 25 '11 18:08

M Sach


People also ask

What is aggregation association and composition in Java?

Association in java is one of the types of relations between classes. It has two forms Aggregation(HAS-A) and Composition(Belongs-to). Aggregation is a relatively weak association, whereas Composition is a strong association. Composition can be called a more restricted form of Aggregation.

What is aggregation and composition with example?

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist. Composition implies a relationship where the child cannot exist independent of the parent.

What is the difference between aggregation and composition in Java example?

In Aggregation, if we delete the parent object, the child object will still exist in the system. For example, if we remove wheels from a car, the car can function adequately with another wheel. In Composition, if we delete a parent object, the child object also gets deleted.

What is Aggrigation in Java?

When an object A contains a reference to another object B or we can say Object A has a HAS-A relationship with Object B, then it is termed as Aggregation. Aggregation helps in reusing the code. Object B can have utility methods and which can be utilized by multiple objects.


1 Answers

You're missing part of the story with Composition. It mandates a couple of extra properties:

  • immutability of the parent (i.e. child can't switch parents)
  • lifecycle responsibility for the children lies with the parent. i.e. the parent is responsible for creating and deleting child instances. Child can't live after parent dies.

You're right that the association will manifest as a reference / collection of references in child & parent respectively. But the code must also enforce the rules above if it's to ensure Composition semantics are enforced.

As for Aggregation: I don't use it. The semantics are too loose and it offers no advantages over straight associations.

hth.

like image 145
sfinnie Avatar answered Sep 28 '22 15:09

sfinnie