Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Association, Aggregation and composition implementation in java

 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware of the concept of association, aggregation and composition however I am not sure how these concepts are actually implemented in java.

I know that these concepts are added using object references however not sure how it controls the life cycle of objects for composition and how the same can be implemented using aggregation?

I did a lot of browsing on this however more I read more i got confused as none of the example is rightly fitted into the definition.

Can someone please explain through the right examples how the concept of association, aggregation and composition are exactly implemented in java.
 
Stephan van Hulst
Bartender
Pie
Posts: 5589
54
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggregation means that an object "has" another object. For instance, a Name could be an aggregation of Strings. The Name will have references to zero or more Strings that together are part of the Name. Aggregation does not imply ownership. While the Name is made up out of these Strings, that doesn't stop the same String instances from being used elsewhere in the program. They are not part of the life cycle of Name. When the Name ceases to exist, the Strings still do.

Composition on the other hand, means that an object "owns" another object. An example could be a Car, which has a private reference to an Engine. The Car is told to move and it in turn delegates it to calls to the Engine. The outside world doesn't know or care about the engine. When the Car is wrecked and picked up by the garbage collector, the Engine goes with it. The Engine is part of the Car's life cycle.
Note that the difference between aggregation and composition is small. If we can open the bonnet and remove the Engine from the Car before it is trashed, so we can put it in another car, this automatically means that the Car isn't composed of an Engine, but it's an aggregation of an Engine.

Association simply means one object has a reference to another object. Aggregation and composition are both types of associations.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.. however I am already aware of these concepts and want to know how these concepts are implemented in java
 
Stephan van Hulst
Bartender
Pie
Posts: 5589
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I implied much of the implementation in my earlier post. Here is an example.

Composition:
Aggregation:
In the case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it's in the Car.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic