Association, Composition and Aggregation - Implementation with java

13,326

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.

Share:
13,326
Sharon Watinsan
Author by

Sharon Watinsan

Updated on June 13, 2022

Comments

  • Sharon Watinsan
    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 a Car.

    2.) Composition - Will not exist if the object is destroyed. for example a Room can't exist without a House.

    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, the Engine 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 like car.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 any accessor mutator functions. and the Object Room has to be created only inside the Constructor. and the instance method that holds the room object must be Private ? 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!

  • Sharon Watinsan
    Sharon Watinsan over 11 years
    Can you give me an example, as in when to use an Association ?
  • Vishal
    Vishal over 11 years
    @sharonHwk I have edited the answer with one link. Hope it will help.