Association, Aggregation and Composition
Relationships
1. Association
It
is a simple structural connection or channel between classes and is a
relationship where all objects have their own lifecycle and there is no owner.
Lets take an example of Department and Student. Multiple students can associate
with a single Department and single student can associate with multiple
Departments, but there is no ownership between the objects and both have their
own lifecycle. Both can create and delete independently. Here is respective
Model and Code for the above example.
2. Aggregation
It
is a specialize form of Association where all object have their own lifecycle
but there is a ownership like parent and child. Child object can not belong to
another parent object at the same time. We can think of it as "has-a"
relationship. Implementation details: 1. Typically we use pointer variables
that point to an object that lives outside the scope of the aggregate class 2.
Can use reference values that point to an object that lives outside the scope
of the aggregate class 3. Not responsible for creating/destroying subclasses
Lets take an example of Employee and Company. A single Employee can not belong
to multiple Companies (legally!! ), but if we delete the Company, Employee
object will not destroy. Here is respective Model and Code for the above
example.
3. Composition
It
is again specialize form of Aggregation. It is a strong type of Aggregation.
Here the Parent and Child objects have coincident lifetimes. Child object dose
not have it's own lifecycle and if parent object gets deleted, then all of it's
child objects will also be deleted
Implementation
details:
1. Typically
we use normal member variables
2. Can
use pointer values if the composition class automatically handles
allocation/deallocation
3. Responsible
for creation/destruction of subclasses
Lets
take an example of a relationship between House and it's Rooms. House can
contain multiple rooms there is no independent life for room and any room can
not belong to two different house. If we delete the house room will also be
automatically deleted. Here is respective Model and Code for the above example.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.