Understanding Association,
Dependency, Aggregation and
Composition
From definition to implementation
1
2
Contents / Agenda
• Introduction
• Scenario and
Requirements
• Extracting information
from requirements
• Inheritance
• Association
• Extracting form
requirements
• Implementation
• Definition
• Aggregation
• Extracting form
requirements
• Implementation
• Definition
• Composition
• Extracting form
requirements
• Implementation
• Definition
• Dependency
• Definition
• Implementation
• UML Diagrams
• Putting it all together
• Summary
3
Introduction
• In this article, we will try to understand three important concepts:
association, aggregation, and composition.
• We will also try to understand in what kind ofscenarios we need
them. Thesethree concepts havereally confused alot of developers
and in this article, my attempt would be to present the concepts in a
simplified manner with some real worldexamples.
• We will look our scenario from real world example and find out
these relationships, then we will implementthem.
4
Scenario and Requirements
• Thewhole point of OOPis that your code replicates real world objects,
thus making your code readable and maintainable. When we sayreal
world, the real world hasrelationships. Let’s consider the simple
requirement listed below:
1. Manager is an employee of XYZlimited corporation.
2. Manager usesaswipe card to enter XYZpremises.
3. Manager hasworkers who work under him.
4. Manager hasthe responsibility of ensuring that the project is successful.
5. Manager's salary will be judged based on projectsuccess.
5
Extracting real world relationships froma
requirements
• If you flesh out the above five point requirement, wecaneasily
visualize four relationships:
• Requirement 1: TheISArelationship | Inheritance
• Requirement 2: TheUsingrelationship | Association
• Requirement 3: TheUsingrelationship with Parent | Aggregation
• Requirement 4 and 5: TheDeath relationship | Composition
• All these requirements will be discussed in next slides with detail.
6
Requirement 1: Inheritance
• If you look at the first requirement (Manager is an Employee ofXYZ
limited corporation), it’s a parent child relationship or inheritance. The
sentence above specifies that Manager is a type of Employee, in other
words we will havetwo classes:
1. Employee (parent class)
2. Manager (child class)
• Weare going to havesimple inheritance relationship between above
classeswith Employee asparent and Manager asits child class.
• Inheritance is an IS-Arelationship, like, Manager IS-AEmployee.
7
Requirement 2: Association
• Requirement 2 is an interesting requirement (Manager usesaswipe
card to enter XYZpremises). In this requirement, the manager object
and the swipe card object useeachother but they havetheir own
object life time. In other words, they canexist without eachother.The
most important point in this relationship is that there is no single
owner.
Requirement 2 : Implementation
8
9
Association Definition
• Theabove diagram showshow the SwipeCardclassusesthe Manager classand
the Manager classusesthe SwipeCardclass.Youcanalso seehow we cancreate
objects of the Manager classand SwipeCardclassindependently and theycan
have their own object life time. Thisrelationship is called the “Association”
relationship.
• In other words, Associationrelationship develops between objects when one
object usesanother object. But, both objects canalso liveindependently.
• Association is aUSINGrelationship, like, Manager USEesSwipeCard.
• Association is also called HAS-Arelationship and it hastwo types:
1. Aggregation : WeakAssociation
2. Composition : Strong Association
10
Requirement 3: Aggregation
• Thethird requirement from our list (Manager hasworkerswho work under
him) denotes the sametype of relationship like association but with a
difference that one of them is an owner. Soasper the requirement, the
Manager object will own Workerobjects.
• Thechild Worker objects cannot belong to anyother object. For instance,a
Worker object cannot belong to aSwipeCardobject.
• But…the Worker object canhaveits own life time which is completely
disconnected from the Manager object. Looking from adifferent
perspective, it meansthat if the Manager object is deleted, the Worker
object doesnot die.
• Thisrelationship is termed asan “Aggregation” relationship.
Requirement 3: Implementation
11
12
Aggregation Definition
• Aggregation is the sameasassociation. Likeassociation, this
relationship develops while one object usesthe other. Additionally,the
objects develop apart-whole relationship and the lifetime of partdoes
not depend on life time ofwhole.
• Aggregation is also aHAS-Arelationship but it contains aPART-WHOLE
relationship, where PARTand WHOLEcanlive separately. For example,
the relationship between aLibrary and Booksis aggregation because
Booksare aPARTof WHOLELibrary, but if we remove Booksfrom
Library, the Library still existsbut its just an empty Library. Similarly,
Bookscanalso exist in some other place.
• Thisis also called WeakAssociation.
13
Requirement 4 & 5:Composition
• Thelast two requirements are actually logically one. If youread
closely,the requirements are asfollows:
1. Manager hasthe responsibility of ensuring thatthe project is successful.
2. Manager's salary will be judged based on projectsuccess.
• Below is the conclusion from analyzing the aboverequirements:
1. Manager and the project objects are dependent on eachother.
2. The lifetimes of both the objects are the same. In other words, the project
will not be successful if the manager is not good, and the manager will not
get good increments if the projecthasissues.
• Thisrelationship is termed asthe “composition” relationship.
Requirement 4 & 5:Implementation
14
15
Composition Definition
• In this relationship, both objects are heavily dependent on each other.
In other words, if one goes for garbage collection the other also hasto
be garbage collected, or putting from adifferent perspective,the
lifetime of the objects are the same. That’s why I have put in the
heading “Death” relationship.
• Composition is also aHAS-Arelationship but it contains aPART-WHOLE
relationship, where PARTand WHOLEcannot live separately. For
example, the relationship between aBookand its Pagesis composition
becausePagesare aPARTof WHOLEBook, but if we remove Pages
from Book, the Bookdoes not existsanymore and soare the pages.
• It is also called StrongAssociation.
16
Dependency Definition
• Dependency is defined asrelation between two classes,where oneclass
depends on another classbut other classmay or not maydepend on the
first class.Soanychangein the one of the class,mayaffect thefunctionality
of the other class,that depends on the first one.
• Comparing with Association:
• Association : both classesuseeachother’s object.
• Dependency: on classusesother class’sobject but other classdoes not usesfirst
class’sobject.
• It is also called aUSINGrelationship.
• Dependency candevelop between objects while they areassociated,
aggregated or composed.Thiskind of relationship develops whileone
object invokes another object’s functionality in order to accomplish some
task.Any change in the called object may break the functionality of the
caller.
Dependency Implementation
• In the following diagram you are using Convert classfrom built inC#
libraries meansyour classis dependent of Convert classand this
relation will be calleddependency.
Similarly you usesMath classesetc.
17
UMLDiagrams
Associatio
n
18
Dependenc
y
Compositio
n
Aggregatio
n
Putting it alltogether
19
Summary
• Toavoid confusion henceforth for these three terms, I haveput
forward atable below which will help uscompare them fromthree
angles: owner, lifetime, and child object.
20

Aggregation

  • 1.
    Understanding Association, Dependency, Aggregationand Composition From definition to implementation 1
  • 2.
    2 Contents / Agenda •Introduction • Scenario and Requirements • Extracting information from requirements • Inheritance • Association • Extracting form requirements • Implementation • Definition • Aggregation • Extracting form requirements • Implementation • Definition • Composition • Extracting form requirements • Implementation • Definition • Dependency • Definition • Implementation • UML Diagrams • Putting it all together • Summary
  • 3.
    3 Introduction • In thisarticle, we will try to understand three important concepts: association, aggregation, and composition. • We will also try to understand in what kind ofscenarios we need them. Thesethree concepts havereally confused alot of developers and in this article, my attempt would be to present the concepts in a simplified manner with some real worldexamples. • We will look our scenario from real world example and find out these relationships, then we will implementthem.
  • 4.
    4 Scenario and Requirements •Thewhole point of OOPis that your code replicates real world objects, thus making your code readable and maintainable. When we sayreal world, the real world hasrelationships. Let’s consider the simple requirement listed below: 1. Manager is an employee of XYZlimited corporation. 2. Manager usesaswipe card to enter XYZpremises. 3. Manager hasworkers who work under him. 4. Manager hasthe responsibility of ensuring that the project is successful. 5. Manager's salary will be judged based on projectsuccess.
  • 5.
    5 Extracting real worldrelationships froma requirements • If you flesh out the above five point requirement, wecaneasily visualize four relationships: • Requirement 1: TheISArelationship | Inheritance • Requirement 2: TheUsingrelationship | Association • Requirement 3: TheUsingrelationship with Parent | Aggregation • Requirement 4 and 5: TheDeath relationship | Composition • All these requirements will be discussed in next slides with detail.
  • 6.
    6 Requirement 1: Inheritance •If you look at the first requirement (Manager is an Employee ofXYZ limited corporation), it’s a parent child relationship or inheritance. The sentence above specifies that Manager is a type of Employee, in other words we will havetwo classes: 1. Employee (parent class) 2. Manager (child class) • Weare going to havesimple inheritance relationship between above classeswith Employee asparent and Manager asits child class. • Inheritance is an IS-Arelationship, like, Manager IS-AEmployee.
  • 7.
    7 Requirement 2: Association •Requirement 2 is an interesting requirement (Manager usesaswipe card to enter XYZpremises). In this requirement, the manager object and the swipe card object useeachother but they havetheir own object life time. In other words, they canexist without eachother.The most important point in this relationship is that there is no single owner.
  • 8.
    Requirement 2 :Implementation 8
  • 9.
    9 Association Definition • Theabovediagram showshow the SwipeCardclassusesthe Manager classand the Manager classusesthe SwipeCardclass.Youcanalso seehow we cancreate objects of the Manager classand SwipeCardclassindependently and theycan have their own object life time. Thisrelationship is called the “Association” relationship. • In other words, Associationrelationship develops between objects when one object usesanother object. But, both objects canalso liveindependently. • Association is aUSINGrelationship, like, Manager USEesSwipeCard. • Association is also called HAS-Arelationship and it hastwo types: 1. Aggregation : WeakAssociation 2. Composition : Strong Association
  • 10.
    10 Requirement 3: Aggregation •Thethird requirement from our list (Manager hasworkerswho work under him) denotes the sametype of relationship like association but with a difference that one of them is an owner. Soasper the requirement, the Manager object will own Workerobjects. • Thechild Worker objects cannot belong to anyother object. For instance,a Worker object cannot belong to aSwipeCardobject. • But…the Worker object canhaveits own life time which is completely disconnected from the Manager object. Looking from adifferent perspective, it meansthat if the Manager object is deleted, the Worker object doesnot die. • Thisrelationship is termed asan “Aggregation” relationship.
  • 11.
  • 12.
    12 Aggregation Definition • Aggregationis the sameasassociation. Likeassociation, this relationship develops while one object usesthe other. Additionally,the objects develop apart-whole relationship and the lifetime of partdoes not depend on life time ofwhole. • Aggregation is also aHAS-Arelationship but it contains aPART-WHOLE relationship, where PARTand WHOLEcanlive separately. For example, the relationship between aLibrary and Booksis aggregation because Booksare aPARTof WHOLELibrary, but if we remove Booksfrom Library, the Library still existsbut its just an empty Library. Similarly, Bookscanalso exist in some other place. • Thisis also called WeakAssociation.
  • 13.
    13 Requirement 4 &5:Composition • Thelast two requirements are actually logically one. If youread closely,the requirements are asfollows: 1. Manager hasthe responsibility of ensuring thatthe project is successful. 2. Manager's salary will be judged based on projectsuccess. • Below is the conclusion from analyzing the aboverequirements: 1. Manager and the project objects are dependent on eachother. 2. The lifetimes of both the objects are the same. In other words, the project will not be successful if the manager is not good, and the manager will not get good increments if the projecthasissues. • Thisrelationship is termed asthe “composition” relationship.
  • 14.
    Requirement 4 &5:Implementation 14
  • 15.
    15 Composition Definition • Inthis relationship, both objects are heavily dependent on each other. In other words, if one goes for garbage collection the other also hasto be garbage collected, or putting from adifferent perspective,the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship. • Composition is also aHAS-Arelationship but it contains aPART-WHOLE relationship, where PARTand WHOLEcannot live separately. For example, the relationship between aBookand its Pagesis composition becausePagesare aPARTof WHOLEBook, but if we remove Pages from Book, the Bookdoes not existsanymore and soare the pages. • It is also called StrongAssociation.
  • 16.
    16 Dependency Definition • Dependencyis defined asrelation between two classes,where oneclass depends on another classbut other classmay or not maydepend on the first class.Soanychangein the one of the class,mayaffect thefunctionality of the other class,that depends on the first one. • Comparing with Association: • Association : both classesuseeachother’s object. • Dependency: on classusesother class’sobject but other classdoes not usesfirst class’sobject. • It is also called aUSINGrelationship. • Dependency candevelop between objects while they areassociated, aggregated or composed.Thiskind of relationship develops whileone object invokes another object’s functionality in order to accomplish some task.Any change in the called object may break the functionality of the caller.
  • 17.
    Dependency Implementation • Inthe following diagram you are using Convert classfrom built inC# libraries meansyour classis dependent of Convert classand this relation will be calleddependency. Similarly you usesMath classesetc. 17
  • 18.
  • 19.
  • 20.
    Summary • Toavoid confusionhenceforth for these three terms, I haveput forward atable below which will help uscompare them fromthree angles: owner, lifetime, and child object. 20