SlideShare a Scribd company logo
1 of 20
Understanding Association,
Dependency, Aggregation and
Composition
From definition to implementation
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 1
Contents / Agenda
• Introduction
• Scenario and Requirements
• Extracting information from
requirements
• Inheritance
• Association
• Extracting form requirements
• Implementation
• Definition
• Aggregation
• Extracting form requirements
• Implementation
• Definition
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 2
• Composition
• Extracting form requirements
• Implementation
• Definition
• Dependency
• Definition
• Implementation
• UML Diagrams
• Putting it all together
• Summary
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 of scenarios we need them.
These three concepts have really confused a lot of developers and in
this article, my attempt would be to present the concepts in a
simplified manner with some real world examples.
• We will look our scenario from real world example and find out these
relationships, then we will implement them.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 3
Scenario and Requirements
• The whole point of OOP is that your code replicates real world objects,
thus making your code readable and maintainable. When we say real
world, the real world has relationships. Let’s consider the simple
requirement listed below:
1. Manager is an employee of XYZ limited corporation.
2. Manager uses a swipe card to enter XYZ premises.
3. Manager has workers who work under him.
4. Manager has the responsibility of ensuring that the project is successful.
5. Manager's salary will be judged based on project success.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 4
Extracting real world relationships from a
requirements
• If you flesh out the above five point requirement, we can easily
visualize four relationships:
• Requirement 1: The IS A relationship | Inheritance
• Requirement 2: The Using relationship | Association
• Requirement 3: The Using relationship with Parent | Aggregation
• Requirement 4 and 5: The Death relationship | Composition
• All these requirements will be discussed in next slides with detail.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 5
Requirement 1: Inheritance
• If you look at the first requirement (Manager is an Employee of XYZ
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 have two classes:
1. Employee (parent class)
2. Manager (child class)
• We are going to have simple inheritance relationship between above
classes with Employee as parent and Manager as its child class.
• Inheritance is an IS-A relationship, like, Manager IS-A Employee.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 6
Requirement 2: Association
• Requirement 2 is an interesting requirement (Manager uses a swipe
card to enter XYZ premises). In this requirement, the 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.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 7
Requirement 2 : Implementation
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 8
Association Definition
• The above diagram shows how the SwipeCard class uses the Manager class and
the Manager class uses the SwipeCard class. You can also see how we can create
objects of the Manager class and SwipeCard class independently and they can
have their own object life time. This relationship is called the “Association”
relationship.
• In other words, Association relationship develops between objects when one
object uses another object. But, both objects can also live independently.
• Association is a USING relationship, like, Manager USEes SwipeCard.
• Association is also called HAS-A relationship and it has two types:
1. Aggregation : Weak Association
2. Composition : Strong Association
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 9
Requirement 3: Aggregation
• The third requirement from our list (Manager has workers who work under
him) denotes the same type of relationship like association but with a
difference that one of them is an owner. So as per the requirement, the
Manager object will own Worker objects.
• The child Worker objects can not belong to any other object. For instance, a
Worker object cannot belong to a SwipeCard object.
• But… the Worker object can have its own life time which is completely
disconnected from the Manager object. Looking from a different
perspective, it means that if the Manager object is deleted, the Worker
object does not die.
• This relationship is termed as an “Aggregation” relationship.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 10
Requirement 3: Implementation
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 11
Aggregation Definition
• Aggregation is the same as association. Like association, this
relationship develops while one object uses the other. Additionally, the
objects develop a part-whole relationship and the lifetime of part does
not depend on life time of whole.
• Aggregation is also a HAS-A relationship but it contains a PART-WHOLE
relationship, where PART and WHOLE can live separately. For example,
the relationship between a Library and Books is aggregation because
Books are a PART of WHOLE Library, but if we remove Books from
Library, the Library still exists but its just an empty Library. Similarly,
Books can also exist in some other place.
• This is also called Weak Association.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 12
Requirement 4 & 5: Composition
• The last two requirements are actually logically one. If you read
closely, the requirements are as follows:
1. Manager has the responsibility of ensuring that the project is successful.
2. Manager's salary will be judged based on project success.
• Below is the conclusion from analyzing the above requirements:
1. Manager and the project objects are dependent on each other.
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 project has issues.
• This relationship is termed as the “composition” relationship.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 13
Requirement 4 & 5: Implementation
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 14
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 has to
be garbage collected, or putting from a different perspective, the
lifetime of the objects are the same. That’s why I have put in the
heading “Death” relationship.
• Composition is also a HAS-A relationship but it contains a PART-WHOLE
relationship, where PART and WHOLE can not live separately. For
example, the relationship between a Book and its Pages is composition
because Pages are a PART of WHOLE Book, but if we remove Pages
from Book, the Book does not exists anymore and so are the pages.
• It is also called Strong Association.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 15
Dependency Definition
• Dependency is defined as relation between two classes, where one class
depends on another class but other class may or not may depend on the
first class. So any change in the one of the class, may affect the functionality
of the other class, that depends on the first one.
• Comparing with Association:
• Association : both classes use each other’s object.
• Dependency : on class uses other class’s object but other class does not uses first
class’s object.
• It is also called a USING relationship.
• Dependency can develop between objects while they are associated,
aggregated or composed. This kind of relationship develops while one
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-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 16
Dependency Implementation
• In the following diagram you are using Convert class from built in C#
libraries means your class is dependent of Convert class and this
relation will be called dependency.
Similarly you uses Math classes etc.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 17
UML Diagrams
Association
Dependency
Composition
Aggregation
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 18
Putting it all together
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 19
Summary
• To avoid confusion henceforth for these three terms, I have put
forward a table below which will help us compare them from three
angles: owner, lifetime, and child object.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 20

More Related Content

What's hot

Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects yndaravind
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)Muhammad Hammad Waseem
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 

What's hot (20)

Er diagrams presentation
Er diagrams presentationEr diagrams presentation
Er diagrams presentation
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 
ER Model in DBMS
ER Model in DBMSER Model in DBMS
ER Model in DBMS
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
ER-Model-ER Diagram
ER-Model-ER DiagramER-Model-ER Diagram
ER-Model-ER Diagram
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Entity relationship modelling
Entity relationship modellingEntity relationship modelling
Entity relationship modelling
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Data independence
Data independenceData independence
Data independence
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Er diagram
Er diagramEr diagram
Er diagram
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Viewers also liked (7)

Difference between association, aggregation and composition
Difference between association, aggregation and compositionDifference between association, aggregation and composition
Difference between association, aggregation and composition
 
Association in oop
Association in oopAssociation in oop
Association in oop
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class Diagrams
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Class diagrams
Class diagramsClass diagrams
Class diagrams
 
Class diagrams
Class diagramsClass diagrams
Class diagrams
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 

More from Mudasir Qazi

Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternMudasir Qazi
 
Database - SQL Joins
Database - SQL JoinsDatabase - SQL Joins
Database - SQL JoinsMudasir Qazi
 
Database - Normalization
Database - NormalizationDatabase - Normalization
Database - NormalizationMudasir Qazi
 
Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)Mudasir Qazi
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - PolymorphismMudasir Qazi
 
OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-valueMudasir Qazi
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPMudasir Qazi
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton PatternMudasir Qazi
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer PatternMudasir Qazi
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMMudasir Qazi
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - IntroductionMudasir Qazi
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternMudasir Qazi
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade PatternMudasir Qazi
 
Design Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityDesign Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityMudasir Qazi
 

More from Mudasir Qazi (14)

Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Database - SQL Joins
Database - SQL JoinsDatabase - SQL Joins
Database - SQL Joins
 
Database - Normalization
Database - NormalizationDatabase - Normalization
Database - Normalization
 
Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - Polymorphism
 
OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-value
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOP
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Design Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityDesign Pattern - Chain of Responsibility
Design Pattern - Chain of Responsibility
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

OOP - Understanding association, aggregation, composition and dependency

  • 1. Understanding Association, Dependency, Aggregation and Composition From definition to implementation 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 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 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 2 • 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 of scenarios we need them. These three concepts have really confused a lot of developers and in this article, my attempt would be to present the concepts in a simplified manner with some real world examples. • We will look our scenario from real world example and find out these relationships, then we will implement them. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 3
  • 4. Scenario and Requirements • The whole point of OOP is that your code replicates real world objects, thus making your code readable and maintainable. When we say real world, the real world has relationships. Let’s consider the simple requirement listed below: 1. Manager is an employee of XYZ limited corporation. 2. Manager uses a swipe card to enter XYZ premises. 3. Manager has workers who work under him. 4. Manager has the responsibility of ensuring that the project is successful. 5. Manager's salary will be judged based on project success. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 4
  • 5. Extracting real world relationships from a requirements • If you flesh out the above five point requirement, we can easily visualize four relationships: • Requirement 1: The IS A relationship | Inheritance • Requirement 2: The Using relationship | Association • Requirement 3: The Using relationship with Parent | Aggregation • Requirement 4 and 5: The Death relationship | Composition • All these requirements will be discussed in next slides with detail. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 5
  • 6. Requirement 1: Inheritance • If you look at the first requirement (Manager is an Employee of XYZ 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 have two classes: 1. Employee (parent class) 2. Manager (child class) • We are going to have simple inheritance relationship between above classes with Employee as parent and Manager as its child class. • Inheritance is an IS-A relationship, like, Manager IS-A Employee. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 6
  • 7. Requirement 2: Association • Requirement 2 is an interesting requirement (Manager uses a swipe card to enter XYZ premises). In this requirement, the 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. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 7
  • 8. Requirement 2 : Implementation 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 8
  • 9. Association Definition • The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time. This relationship is called the “Association” relationship. • In other words, Association relationship develops between objects when one object uses another object. But, both objects can also live independently. • Association is a USING relationship, like, Manager USEes SwipeCard. • Association is also called HAS-A relationship and it has two types: 1. Aggregation : Weak Association 2. Composition : Strong Association 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 9
  • 10. Requirement 3: Aggregation • The third requirement from our list (Manager has workers who work under him) denotes the same type of relationship like association but with a difference that one of them is an owner. So as per the requirement, the Manager object will own Worker objects. • The child Worker objects can not belong to any other object. For instance, a Worker object cannot belong to a SwipeCard object. • But… the Worker object can have its own life time which is completely disconnected from the Manager object. Looking from a different perspective, it means that if the Manager object is deleted, the Worker object does not die. • This relationship is termed as an “Aggregation” relationship. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 10
  • 11. Requirement 3: Implementation 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 11
  • 12. Aggregation Definition • Aggregation is the same as association. Like association, this relationship develops while one object uses the other. Additionally, the objects develop a part-whole relationship and the lifetime of part does not depend on life time of whole. • Aggregation is also a HAS-A relationship but it contains a PART-WHOLE relationship, where PART and WHOLE can live separately. For example, the relationship between a Library and Books is aggregation because Books are a PART of WHOLE Library, but if we remove Books from Library, the Library still exists but its just an empty Library. Similarly, Books can also exist in some other place. • This is also called Weak Association. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 12
  • 13. Requirement 4 & 5: Composition • The last two requirements are actually logically one. If you read closely, the requirements are as follows: 1. Manager has the responsibility of ensuring that the project is successful. 2. Manager's salary will be judged based on project success. • Below is the conclusion from analyzing the above requirements: 1. Manager and the project objects are dependent on each other. 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 project has issues. • This relationship is termed as the “composition” relationship. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 13
  • 14. Requirement 4 & 5: Implementation 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 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 has to be garbage collected, or putting from a different perspective, the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship. • Composition is also a HAS-A relationship but it contains a PART-WHOLE relationship, where PART and WHOLE can not live separately. For example, the relationship between a Book and its Pages is composition because Pages are a PART of WHOLE Book, but if we remove Pages from Book, the Book does not exists anymore and so are the pages. • It is also called Strong Association. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 15
  • 16. Dependency Definition • Dependency is defined as relation between two classes, where one class depends on another class but other class may or not may depend on the first class. So any change in the one of the class, may affect the functionality of the other class, that depends on the first one. • Comparing with Association: • Association : both classes use each other’s object. • Dependency : on class uses other class’s object but other class does not uses first class’s object. • It is also called a USING relationship. • Dependency can develop between objects while they are associated, aggregated or composed. This kind of relationship develops while one 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-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 16
  • 17. Dependency Implementation • In the following diagram you are using Convert class from built in C# libraries means your class is dependent of Convert class and this relation will be called dependency. Similarly you uses Math classes etc. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 17
  • 19. Putting it all together 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 19
  • 20. Summary • To avoid confusion henceforth for these three terms, I have put forward a table below which will help us compare them from three angles: owner, lifetime, and child object. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 20