This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Association, Aggregation and composition implementation in java  RSS feed

 
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.
 
Bartender
Posts: 9545
188
  • 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
Posts: 9545
188
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!