Wednesday, July 22, 2015

Association, Aggregation and Composition

Association - Indicates the relationship between objects.
                       Example : Computer uses keyboard as input device.
                       An association is used when one object wants another object to perform a service for it.


Aggregation - Is a special case of association, a directional association between objects. When an
                        object "has-a" another object, then you have got an aggregation between them.
                        Example : Room has a table, but the table can exist without the room.


                        public class Room
                        {
                           private Table table;
  
                           void setTable(Table table)
                           {
                              this.table=table;
                            }
                        }


Composition - Is a special case of aggregation. Composition is more restrictive. When there is a
                         composition between two objects, the composed object cannot exist without the outer 
                         object.This restriction is not there in aggregation.
                         Example : Rooms in a house, which cannot exist after the lifetime of the house.


                         public class House
                         {
                           private Room room;
 
                           House(Room roomSpecks)
                          {
                               room = new Room (roomSpecks);
                           }
                         }

No comments:

Post a Comment

Home