UML: Difference between Association, Aggregation and Composition

simplify
simplify @Image courtesy of digitalart/ FreeDigitalPhotos.net

UML: Difference between Association, Aggregation and Composition

Welcome to CodeSpread!

Association

  • It is a relationship between objects.
  • One object is connected to the other.
  • Usually called as “is-a” relationship.
  • Both objects have independent life-cycle.
  • Each object owns their actions and will not affect other object.

public class Car

{

// Implementation

}

public class Driver

{

void Drive( Car car){

//Implementation

}

}

In the above example, a driver object is associated with a car as it has a function to drive a car object.

Both the objects have independent life cycle.

Aggregation

  • Specialized form of Association.
  • Usually called as “has-a” relationship.
  • Each object has an independent life-cycle.
  • A whole-part relationship between a component object and an aggregate object.
  • Sense of ownership between objects.

public class Mouse

{

// Implementation

}

public class Keyboard

{

// Implementation

}

public class Computer

{

private  Mouse mouse;

private  Keyboard keyboard;

void Computer (Mouse mouse, Keyboard keyboard){

//Implementation

}

}

 

In the above example, object computer is made up of objects like mouse and keyboard but the existence of mouse and keyboard is not dependent on computer object.

Object Computer has a sense of ownership towards each component.

All the objects have independent life cycle.

Composition

  • Specialized form of Aggregation.
  • Usually called as “has-a” relationship.
  • Child Object has dependent life-cycle. This is what separates it from aggregation.
  • A whole-part relationship between a component object and an aggregate object.
  • Sense of ownership between objects.

public class Classroom

{

// Implementation

}

public class Library

{

// Implementation

}

public class College

{

Classroom classroom= new Classroom();

Library library= new Library();

}

}

 

In the above example, object College is made up of objects like classroom and library and the existence of classroom and library is directly dependent on College object.

If you have anything to add/share, please mail it to admin@codespread.com

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • 20 Dec: Must Read Codes [6-10]20 Dec: Must Read Codes [6-10]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • Magic Wand: Object-relational mappingMagic Wand: Object-relational mappingAs a developer, we are always looking for reusable components which can ease our life and I can suggest ORM as the developer's magic wand because it wipes off the overhead of dealing directly with ...
  • jQuery: Effects/MethodsjQuery: Effects/MethodsIn our last article, jQuery Part 1 , we covered the basic understanding and now we will move to Methods or effects available in jQuery. Hide/Show/Toggle Methods The most used method Hide/Show. Fr...
  • Code 1: OOPS, class or object.Code 1: OOPS, class or object."I am not a computer science graduate" Yes! This was my innocent submission when i started my career in IT. I must admit it now that i did use this as an excuse and so do many people to avoid...
  • Creation of DemoSpaceCreation of DemoSpaceThe Backend URL shown in the article : NetBiscuits: How fascinating can it be to create a mobile website? is the Demospace server location where the Published files of the applications will be plac...
  • Split the Web.ConfigSplit the Web.ConfigWhen we work on web projects, we usually encounter with web.config files, not one but may be many at the same time. These web.config files are ASP.NET configuration files. They can be distributed t...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...