Anda di halaman 1dari 27

Object

Relationships
Object oriented Relationship
In Object Oriented world, if one class has to
communicate with any other class (or classes),
it has to communicate by using one of the
Object Oriented Relationships.
There are three (3) OO Relationships,
Association
Composition
Aggregation
Weak
Strong (Also called as Composition or Containment)
Inheritance or Generalization
Association
Association represents a general binary
relationship that describes an activity between
two classes.
An association is illustrated using a solid line
between the two classes with an optional label
that describes the relationship.
Each class involved in the relationship may have
a role name played by the class in the
relationship.
Examples :
A student taking a course is an association between
the Student class and the Course class.
A faculty member teaching a course is an association
between the Faculty class and the Course class.
Class diagram
Composition of objects
In real-life, complex objects are often built from
smaller, simpler objects.
For example, a car is built using a metal frame,
an engine, some tires, a transmission, a steering
wheel, and a large number of other parts.
This process of building complex objects from
simpler ones is called object composition.
Continue
Broadly speaking, object composition models a has-a
relationship between two objects.
For example: A car has-a transmission. Our computer
has-a CPU. We have-a heart.
The complex object is sometimes called the whole, or
the parent.
The simpler object is often called the part, child, or
component.
In C++, structs and classes can have data members of
various types. When we build classes with data
members, were essentially constructing a complex
object from simpler parts, which is object composition.
For this reason, structs and classes are sometimes
referred to as composite types.
Continue
Object Composition is useful in a C++ context
because it allows us to create complex classes
by combining simpler, more easily
manageable parts.
This reduces complexity, and allows us to
write code faster and with less errors because
we can reuse code that has already been
written, tested, and verified as working.
Definition of Composition
Composition is a way to combine simple objects or
data types into more complex ones.
To qualify as a composition, an object and a part
must have the following relationship:
1. The part (member) is part of the object (class)
2. The part (member) can only belong to one
object (class) at a time
3. The part (member) has its existence managed
by the object (class)
4. The part (member) does not know about the
existence of the object (class)
Continue
Composition relationships are part-whole
relationships where the part must constitute
part of the whole object.
For example, a heart is an part of a persons
body.
The part in a composition can only be part of
one object at a time. A heart that is part of
one persons body can not be part of someone
elses body at the same time.
Continue
In a composition relationship, the object is
responsible for the existence of the parts.
Most often, this means the part is created when the
object is created, and destroyed when the object is
destroyed.
But more broadly, it means the object manages the
parts lifetime in such a way that the user of the
object does not need to get involved.
For example, when a body is created, the heart is
created too. When a persons body is destroyed,
their heart is destroyed too. Because of this,
composition is sometimes called a death
relationship.
Continue
And finally, the part doesnt know about the existence of
the whole. Your heart operates blissfully unaware that it is
part of a larger structure. We call this a unidirectional
relationship, because the body knows about the heart, but
not the other way around.
Note that composition has nothing to say about the
transferability of parts. A heart can be transplanted from
one body to another. However, even after being
transplanted, it still meets the requirements for a
composition.
The parts of a composition can be singular or multiplicative.
For example, a heart is a singular part of the body, but a
body contains 10 fingers (which could be modeled as an
array).
Aggregation
Like a composition, an aggregation is still a part-
whole relationship, where the parts are contained
within the whole, and 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.
When an aggregation is created, the aggregation is
not responsible for creating the parts. When an
aggregation is destroyed, the aggregation is not
responsible for destroying the parts.
Continue
To quality as an aggregation, a whole object
and its parts must have the following
relationship:
1. The part (member) is part of the object (class)
2. The part (member) can belong to more than one
object (class) at a time
3. The part (member) does not have its existence
managed by the object (class)
4. The part (member) does not know about the
existence of the object (class)
Properties of Aggregation
Aggregation is a variant of the \has a" or
association relationship; aggregation is more
specific than association.
It is an association that represents a part-whole or
part-of relationship.
Aggregation can occur when a class is a collection
or container of other classes, but where the
contained classes do not have a strong life cycle
dependency on the container-essentially, I if the
container is destroyed, its contents are not.
Weak Aggregation
Weak Aggregation represents an ownership
relationship between two classes. Aggregation
models the relationship like has-a.
Example
Customer has bank account.
Room has chairs.
Strong Aggregation
Also called composition.
Composition is a stronger form of aggregation. In
a composite object the components exists only as
part of the composite.
Rules of Thumb
The whole and parts have coincident lifetimes.
Both classes represent physical items.

Examples
An engine is a part a car.
A package is a part of a shipment.
Employee is a part of a team.
Example 1
Consider the relationship between a person and
their home address. In this example, for simplicity,
well say every person has an address. However,
that address can belong to more than one person
at a time: for example, to both you and your
roommate or significant other. However, that
address isnt managed by the person -- the address
probably 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 dont know what people live there.
Therefore, this is an aggregate relationship.
Example 2
consider a car and an engine. A car engine is
part of the car. And although the engine
belongs to the car, it can belong to other
things as well, like the person who owns the
car. The car is not responsible for the creation
or destruction of the engine. And while the
car knows it has an engine (it has to in order
to get anywhere) the engine doesnt know its
part of the car.
Summarizing composition and
aggregation
Compositions:
Typically use normal member variables
Can use pointer values if the composition class
automatically handles allocation/deallocation
Responsible for creation/destruction of parts
Parts belongs to only one object at a time.
Aggregations:
Typically use pointer or reference members that point
to or reference objects that lives outside the scope of
the aggregate class
Not responsible for creating/destroying parts
Parts belongs to more than one object at a time.
Inheritance (Generalization)
Inheritance is a code reuse mechanism to build new
objects out of old ones.
I Inheritance defines a relationship among classes
where one or more classes share the behavior
and/or attributes of another class.
For example, a class called ink-jet printer inherits all
the behaviour and attributes of the class computer
printer.
Inheritance also enables `polymorphism'.
The relationship superclass and subclass is used.
Each subclass is a specialized version of its
superclass.
Deferent kinds of objects often have a certain
amount in common with each other.
Mountain bikes, road bikes and tandem bikes, for
example, all share the characteristics of bicycles
(current speed, current pedal cadence, current gear).
Additional features that make them deferent: tandem
bicycles have two seats and two sets of handlebars;
road bikes have drop handlebars; some mountain
bikes have an additional chain ring, giving them a
lower gear ratio.
Object-oriented programming allows classes to
inherit commonly used state and behavior from
other classes.
In this example, Bicycle now becomes the superclass
of MountainBike, RoadBike, and TandemBike.

Anda mungkin juga menyukai