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

What's hot (20)

Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Difference between association, aggregation and composition
Difference between association, aggregation and compositionDifference between association, aggregation and composition
Difference between association, aggregation and composition
 
Type conversion
Type conversionType conversion
Type conversion
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOP
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Data types in java
Data types in javaData types in java
Data types in java
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 

Viewers also liked

Viewers also liked (6)

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
 
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 (13)

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
 
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

(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
 
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
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
(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
 
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
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
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
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

(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...
 
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
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(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
 
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
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
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...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

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