Association Aggregation-Final
Association Aggregation-Final
ASSOCIATION, AGGREGATION
AND COMPOSITION
Abinaya 18E102
Samyuktha V 18E145
ASSOCIATION
ASSOCIATION
Association, aggregation, and composition to define
relationships between the objects in your application
That means that in this Association (Aggregation) the object has their own
life cycle.
The owner and child objects cannot belong to a different parent object.
ASSOCIATION
To qualify as an association, an object and another object must have the following
relationship:
At the same time, one patient can visit multiple doctors for treatment or consultation. Each of
these objects has its own life cycle and there is no “owner” or parent.
The objects that are part of the association relationship can be created and destroyed
independently.
EXAMPLE 2
For example Managers and Employees, multiple employees may be associated with a single
manager and a single employee may be associated with multiple managers.
COMPARISON
ASSOCIATION AGGREGATION COMPOSITION
LIFETIME Have their own life time Have their own life time Owners life time
CHILD OBJECT No child objects all are Child objects belong to Child objects belong to
independent single parent single parent
ASSOCIATION
Unlike a composition or aggregation, where the part is a part of the whole object, in an
association, the associated object is otherwise unrelated to the object.
Just like an aggregation, the associated object can belong to multiple objects simultaneously, and
isn’t managed by those objects.
Because associations are a broad type of relationship, they can be implemented in many different
ways.
However, most often, associations are implemented using pointers, where the object points at
the associated object.
In this example, we’ll implement a bi-directional Doctor/Patient relationship, since it makes
sense for the Doctors to know who their Patients are, and vice-versa.
#include <cstdint>
#include <functional> // reference_wrapper
#include <iostream>
#include <string>
#include <vector>
// Since Doctor and Patient have a circular dependency, we're
going to forward declare Patient
class Patient;
class Doctor
{
private:
std::string m_name{};
std::vector<std::reference_wrapper<const Patient>>
m_patient{};
public:
Doctor(const std::string& name) :
m_name{ name }
{
}
void addPatient(Patient& patient);
// We'll implement this function below Patient since we
need Patient to be defined at that point
friend std::ostream& operator<<(std::ostream &out,
const Doctor &doctor);
const std::string& getName() const { return m_name; }
};
class Patient
{
private:
std::string m_name{};
std::vector<std::reference_wrapper<const Doctor>> m_doctor{};
// so that we can use it here
// We're going to make addDoctor private because we don't want
the public to use it.
// They should use Doctor::addPatient() instead, which is
publicly exposed
void addDoctor(const Doctor& doctor)
{
m_doctor.push_back(doctor);
}
public:
Patient(const std::string& name)
: m_name{ name }
{
}
// We'll implement this function below Doctor since we need Doctor to
be defined at that point
friend std::ostream& operator<<(std::ostream &out, const Patient
&patient);
const std::string& getName() const { return m_name; }
// We'll friend Doctor::addPatient() so it can access the private function
Patient::addDoctor()
friend void Doctor::addPatient(Patient& patient);
};
void Doctor::addPatient(Patient& patient)
{
// Our doctor will add this patient
m_patient.push_back(patient);
// and the patient will also add this doctor
patient.addDoctor(*this);
}
std::ostream& operator<<(std::ostream &out, const
Patient &patient)
{
if (patient.m_doctor.empty())
{
out << patient.getName() << " has no doctors right
now";
return out;
}
A doctor can see many patients in a day, and a patient can see many doctors (perhaps they want
a second opinion, or they are visiting different types of doctors).
Neither of the object’s lifespans are tied to the other.We can say that association models as “uses-
a” relationship.
The doctor “uses” the patient (to earn income). The patient uses the doctor (for whatever health
purposes they need).
REFLEXIVE ASSOCIATION
Sometimes objects may have a relationship with other objects of the same type.
This is called a reflexive association.
A good example of a reflexive association is the relationship between a university course
and its prerequisites (which are also university courses).
Consider the simplified case where a Course can only have one prerequisite. We can do
something like this:
This can lead to a chain of associations (a course has a prerequisite, which has a
prerequisite)
ASSOCIATIONS CAN BE INDIRECT
In all of the previous cases, we’ve used either pointers or references to directly link objects
together.
However, in an association, this is not strictly required. Any kind of data that allows you to
has its own life cycle but there exists an ownership as well.
Aggregation is a typical whole/part or parent/child relationship but it may or may not denote
physical containment.
An essential property of an aggregation relationship is that the whole or parent (i.e. the owner) can
We can say it is a direct association among the objects. In Aggregation, the direction specifies
However, if an employee’s department is deleted, the employee object would not be destroyed
but would live on.
Note that the relationships between objects participating in an aggregation cannot be reciprocal
—i.e., a department may “own” an employee, but the employee does not own the department.
EXAMPLE 2
For example, departments and employees, a department has many employees but a single
employee is not associated with multiple departments.
UML Representation of Aggregation (white diamond):
The UML representation of the example above (relation between employee and department):
Aggregation implementation
To qualify as an aggregation, a whole object and its parts must have the
following relationship:
The part (member) can belong to more than one object (class) at a time
The part (member) does not have its existence managed by the object (class)
The part (member) does not know about the existence of the object (class)
AGGREGATION
Like a composition, an aggregation is still a part-whole relationship, where the parts are
It is a unidirectional relationship.
However, unlike a composition, parts can belong to more than one object at a time, and
the whole object is not responsible for the existence and lifespan of the parts.
AGGREGATION
When an aggregation is created, the aggregation is not responsible for
However, these member variables are typically either references or pointers that are
used to point at objects that have been created outside the scope of the class.
constructor parameters, or it begins empty and the sub-objects are added later via
not deleted).
address.
In this example, for simplicity, we’ll say every person has an address.
However, that address can belong to more than one person at a time, for
existed before the person got there, and will exist after the person is gone.
Additionally, a person knows what address they live at, but the addresses
relationship.
Example - 2
Example - 3
Consider a car and an engine.
And although the engine belongs to the car, it can belong to other things as
And while the car knows it has an engine (it has to in order to get
Second, the teacher will be unaware of what department they’re part of.
In C++
Code Explaination
In this case, bob is created independently of department, and then passed
into department‘s constructor.
But the teacher itself is not destroyed, so it still exists until it is independently
each other.
Most often, this means the part is created when the object is created, and destroyed
But more broadly, it means the object manages the part’s lifetime in such a way that
This a unidirectional relationship
Composition
Member-objects of a class are constructed:
initialization list
The part (member) can only belong to one object (class) at a time
The part (member) has its existence managed by the object (class)
The part (member) does not know about the existence of the object (class)
Example - 1
A good real-life example of a composition is the relationship between a
When a body is created, the brain is created too. When a person’s body is
screen.
One thing that all of these creatures/objects have in common is that they all have a location.
In this example, we are going to create a creature class that uses a point class to hold the
creature’s location.
Example - 3
First, let’s design the point class.
Our creature is going to live in a 2d world, so our point class will have 2
dimensions, X and Y.
The creature’s name and location have one parent, and their lifetime is tied to
The numerator and denominator are part of the Fraction (contained with in
it).
hold integers.
When a Fraction instance is created, the numerator and denominator are created.
When the fraction instance is destroyed, the numerator and denominator are
destroyed as well.
Association vs Aggregation vs
Composition
THANK YOU!