Association, Composition and Aggregation - Implementation with java
Solution 1
The type of the association has little impact on how you implement it. The fact that it's an aggregation or a composition is only a semantic difference.
The difference appears if the objects are persisted in some database. In this case, if the container object is removed from the database, the contained object is also removed or not, depending on the type of the association.
Solution 2
To define aggregation association we take another example.... A person and a company...
A person and a company are two individual entities, but a person can work in a company and he is called as an employee... So employee has an existence in outer world as an person. So even if company perishes, but person will not perish...
public class Example {
public static void main(String[] args) {
Person p1 = new Person("Vishal");
Company abc = new Company(p1);
}
}
class Person{
String name;
public Person(String name) {
this.name = name;
}
}
class Company{
Person employee;
public Company(Person p1) {
employee = p1;
}
}
I did not go into the actual definitions of the company and person, but I think it will sense now.
Whereas composition association is an association between a company and a department. As soon as company perish, it's department does not make any sense, so it must perish...
Another example of composition association is order and different items of the order. You order few books(3) from an online store. In this case 3 order lines(items) must be cancelled if order is cancelled. So existence of order lines depends only on order.
Manager uses a swipe card to enter XYZ premises. Manager object and the swipe card object use each other but they have their own object life time. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner.
Whereas in aggregation one object owns another object...
It is a nice link on these 3 concepts... For association you can check 2nd requirement... http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit
Solution 3
Further to Vishal's answer, you could have an Employee class extending Person class, so that Company holds an Employee instead of a Person and when the company is destroyed, the Employee goes with it (collected as garbage) and not a Person. This is just to be more analagous to reality and may not be necessary.
![Sharon Watinsan](https://i.stack.imgur.com/zVpOf.jpg?s=256&g=1)
Sharon Watinsan
Updated on June 13, 2022Comments
-
Sharon Watinsan about 2 years
I'm a bit confused about
Association, Aggregation and Composition
. Even though loads of website, and forums discusses this topic, i have become more confused after reading some of them.I want to know if the following are correct :
1.) Aggregation - WIll exist if the Whole is destroyed. for example an
Engine
could exist with or without aCar
.2.) Composition - Will not exist if the object is destroyed. for example a
Room
can't exist without aHouse
.3.) Association - I am not sure, at what instances we should use this. Can someone comment on this.
When it comes to writing Java codes for Aggregation, COmposition and Association
4.) Aggregation
Class Car { private Engine engine; public void setEngine(Engine engine){ this.engine=engine; } public Engine getEngine(){ return engine; } }
I think that if it's an Aggregation association, then there should be
accessors and mutators
defined. Is this correct ?According to my definition of Aggregation, i mentioned that if the
Car
object is destroyed, theEngine
can be accessed. But, i don't see that hapenning in the above code. If Car object is destroyed, there will not be any way to access the Engine likecar.getEngine()
. So how does this happen ?Composition
Public House { private Room room; Public House (){ room = new Room (int noRooms, String nameOfHouse); } }
I think that if it's a
C0mposition
, then there should not be anyaccessor mutator
functions. and the ObjectRoom
has to be created only inside the Constructor. and the instance method that holds theroom
object must bePrivate
? Am i correct ?note: Once again, i have googled, and visited many websites/forums trying to clear my doubts.Similar questions were posted in many forums, but yet i only got more confused about this. Can someone help me clear my doubts. Thank you!
-
nawfal about 11 yearswell almost: stackoverflow.com/questions/9862286/…
-
-
Sharon Watinsan over 11 yearsCan you give me an example, as in when to use an
Association
? -
Vishal over 11 years@sharonHwk I have edited the answer with one link. Hope it will help.