SlideShare a Scribd company logo
1 of 20
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

More Related Content

Similar to Aggregation

SAD02 - Object Orientation
SAD02 - Object OrientationSAD02 - Object Orientation
SAD02 - Object OrientationMichael Heron
 
Objects and Classes.pptx
Objects and Classes.pptxObjects and Classes.pptx
Objects and Classes.pptxBalasundaramSr
 
Objects and Classes.pptx
Objects and Classes.pptxObjects and Classes.pptx
Objects and Classes.pptxBalasundaramSr
 
Objects and Classes BRIEF.pptx
Objects and Classes BRIEF.pptxObjects and Classes BRIEF.pptx
Objects and Classes BRIEF.pptxBalasundaramSr
 
Structural modeling and analysis
Structural modeling and analysisStructural modeling and analysis
Structural modeling and analysisJIGAR MAKHIJA
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptxsandeep54552
 
Slide 5 Class Diagram
Slide 5 Class DiagramSlide 5 Class Diagram
Slide 5 Class DiagramNiloy Rocker
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented ProgramMichael Heron
 
L1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdfL1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdfBhanuJatinSingh
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UMLVeneet-BA
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UMLVeneet-BA
 
IBM OOAD Part1 Summary
IBM OOAD Part1 SummaryIBM OOAD Part1 Summary
IBM OOAD Part1 SummaryHaitham Raik
 
UML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptUML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptWorkDrive2
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! EdholeEdhole.com
 
Design Pattern Explained CH8
Design Pattern Explained CH8Design Pattern Explained CH8
Design Pattern Explained CH8Jamie (Taka) Wang
 
06 class diagrams
06 class diagrams06 class diagrams
06 class diagramsBaskarkncet
 

Similar to Aggregation (20)

SAD02 - Object Orientation
SAD02 - Object OrientationSAD02 - Object Orientation
SAD02 - Object Orientation
 
Objects and Classes.pptx
Objects and Classes.pptxObjects and Classes.pptx
Objects and Classes.pptx
 
Objects and Classes.pptx
Objects and Classes.pptxObjects and Classes.pptx
Objects and Classes.pptx
 
Objects and Classes BRIEF.pptx
Objects and Classes BRIEF.pptxObjects and Classes BRIEF.pptx
Objects and Classes BRIEF.pptx
 
07 intro2 oop
07 intro2 oop07 intro2 oop
07 intro2 oop
 
Intro to UML
Intro to UMLIntro to UML
Intro to UML
 
Structural modeling and analysis
Structural modeling and analysisStructural modeling and analysis
Structural modeling and analysis
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
 
Slide 5 Class Diagram
Slide 5 Class DiagramSlide 5 Class Diagram
Slide 5 Class Diagram
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program
 
L1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdfL1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdf
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UML
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UML
 
IBM OOAD Part1 Summary
IBM OOAD Part1 SummaryIBM OOAD Part1 Summary
IBM OOAD Part1 Summary
 
UML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptUML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.ppt
 
UML-class_diagram.ppt
UML-class_diagram.pptUML-class_diagram.ppt
UML-class_diagram.ppt
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
 
Design Pattern Explained CH8
Design Pattern Explained CH8Design Pattern Explained CH8
Design Pattern Explained CH8
 
06 class diagrams
06 class diagrams06 class diagrams
06 class diagrams
 
Hibernate using jpa
Hibernate using jpaHibernate using jpa
Hibernate using jpa
 

More from zindadili

Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Virtual function
Virtual functionVirtual function
Virtual functionzindadili
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Polymorphism
PolymorphismPolymorphism
Polymorphismzindadili
 
Function overloading
Function overloadingFunction overloading
Function overloadingzindadili
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritancezindadili
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritancezindadili
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritancezindadili
 
Abstraction1
Abstraction1Abstraction1
Abstraction1zindadili
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Friend function
Friend functionFriend function
Friend functionzindadili
 

More from zindadili (20)

Namespaces
NamespacesNamespaces
Namespaces
 
Namespace1
Namespace1Namespace1
Namespace1
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Templates2
Templates2Templates2
Templates2
 
Templates1
Templates1Templates1
Templates1
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
 
Abstraction
AbstractionAbstraction
Abstraction
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Inheritance
InheritanceInheritance
Inheritance
 
Friend function
Friend functionFriend function
Friend function
 
Enum
EnumEnum
Enum
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 

Aggregation

  • 1. Understanding Association, Dependency, Aggregation and 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 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. 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 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. 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 • 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. 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.
  • 12. 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. 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 • 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. 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.
  • 17. 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
  • 20. 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