Wednesday, September 24, 2008
RELATIONSHIP BETWEEN OBJECTS
Association
In an object-oriented solution, we find an organisation of objects, each object playing a
set of roles and each role handling a set of responsibilities.
Playing a role, a person has some contract with the external world to provide a set of
services. A taxicab driver plays his role and is responsible to ply passenger for a fair
fixed by the authorities. If a person approaches a taxi in a stand or calls one by radio, the
taxi cab driver or taxi company is obliged to transport the person from the origin to a
desired destination. To request for the services of a taxicab, we have to associate with the
taxi driver by making eye contact or calling him over a phone. We cannot request a
service from an entity before establishing a contact, i.e., before forming an association .
In the society, an entity implicitly or explicitly announces the services offered by it. An
object, which is a model of a real world entity, exposes its interfaces announcing to other
objects the services it offers. Viewing differently, the interfaces announce the
responsibilities of an object. Another object in the application that can establish an
association with this object can send a message (aka request) requesting the object for
some services.
Thus, association between two objects is necessary before they can communicate with
each other. When an object sends a message to a second and for its own behavior
depends on the resulting state of the second object that has acted on the request or the on
the value returned, there is a dependency by the first object on the second.
Whole-Part Relationship – A Special Type of Association
In an association, an object seeks services of another object to achieve a result that it
could not have achieved on its own. But, the modeling of the object itself is complete.
When an object’s own definition does not structurally depend on the associated object,
but has a reference to the other object, we call the relationship as association.
There are several cases where we build an object by assembling other objects. We form a
whole by using different parts, though the parts themselves may be useful as independent
objects. A taxicab has parts like steering system, braking system, engine, body, wheels
etc. To behave like a taxicab, it needs all the parts. For the cab to move faster, the engine
has to accelerate. A passenger asks the driver to move faster or slower; the taxi sends a
message through the accelerator pedal to the engine to rotate faster or slower. We don’t
say that the engine is rotating faster, but that the car is speeding. Seen from outside, we
see the whole object, and the parts are hidden. The structure and behavior of the whole
depends on the parts. When a behavior is requested, the whole delegates the
responsibility to appropriate parts.
The whole is aware of its parts as it requests for different services from the parts. A part
need not be aware of the whole. It would respond to the requests from the whole. Being
part of the whole, and not being visible to other objects, it receives requests only from the
whole whose part it is.
Whole-part relationships are classified as ‘aggregation’ and ‘composition ’ based on how
closely the parts relate to the whole.
Aggregation
When the parts are not created for the sake of whole and can be interchanged during the
life of the whole, it is aggregation. The parts could be manufactured separately and
assembled to create the whole. Parts of an automobile are manufactured separately and
are changed during the life of the vehicle. Also, a part may be used with another whole –
Explaining Object Oriented Analysis Concepts to Managers of an Organization
19
a wheel that is used in a car may be used in a bullock cart too( Seven years ago, I went to
buy a tyre for my Maruti car. The sh op keeper asked if I needed the tyre for a bullock
cart. He said that no tax would be levied on this purchase, if it was for a bullock cart).
In some cases, a part may be shared by several whole objects simultaneously. If we
consider a sports team with several players, these players may be members of several
teams at a time. Several computers may share one monitor. It is possible to use a TV set
as a TV receiver and an AV player for DVD.
As the parts are not created for the sake of whole and also may be shared, the whole does
not have the responsibility of creating the parts and does not have right to delete the parts.
The whole does not own its parts.
Composition
Composition is a special kind of aggregation.
The tyre of a car is manufactured using steel re-enforcer and rubber. We do not
recompose the parts during the use of the tyre nor do we separate the parts after it is
manufactured. When we compose a word, we use several characters and when the word
is deleted all the characters in the word are also deleted. In composition, the parts are
created for the whole and are deleted with the whole.
In a composition relationship, the whole owns its parts; it has the responsibility to create
and delete the parts. Hence, the parts cannot be shared with other whole objects.
Model of a whole-part relationship (aggregation Vs. composition) depends on the domain
and context. E.g., for an end user, a laptop computer would be composed of its parts and
a desktop computer aggregation of its parts. For a manufacturer of the PCs, laptop and
desktop computer both would be aggregation.
Inheritance
Often we use words in a way that the meaning of one concept is part of another. If we say
that a person travels by an automobile, the meaning does not change if he travels by a car,
bus, or any other automobile vehicle. In our thought process, we use a grouping
mechanism to understand and use concepts. This is a way of knowing something new in
terms of something we know. What we already know has the essential features to
represent the concept at that level. Without this process we could not build knowledge.
When we “buy vegetables, fruits, groceries, and clothes” we have generalized some
concepts.
When we say that a car is an automobile, we understand that all the structural and
behavioral properties of the concept of automobile are applicable to a car. We do not
have to define and understand these concepts again. This simplifies our understanding
and communication. The car can have some additional property, uncommon to other
automobiles. It is easier to say that a car is a special kind of automobile having all the
properties of a common automobile and some stated additional features. This extends the
concept of an automobile. When we define a bus, again we can use the already defined
model of automobile and add features specific to a bus.
When we define a new concept in terms of already understood concepts, we inherit the
features of the previously known concept and specialize it by adding some new features.
As we have inherited some features of the existing model, should there be some changes
to the original concepts, it would affect all the specialized concept too. E.g., if the
concept of automobile assumed it to run on IC engines, all the inherited vehicles would
Dinesha and Bhat
20
also have IC engines – not battery powered motors to move them. If there be a change in
the concept of automobiles to change from IC engine to electrical motors, it would affect
all the inherited concepts.
With inheritance we can define a new concept in terms of already known concepts, which
helps build knowledge incrementally, reusing existing knowledge/model.
Contrast this with association where we use another object by knowing its responsibilities
and interfaces.
Subscribe to:
Post Comments (Atom)
About Me
- Senthilkumar
- Bangalore, Karnataka, India
1 comment:
top [url=http://www.c-online-casino.co.uk/]uk casino bonus[/url] coincide the latest [url=http://www.realcazinoz.com/]casino games[/url] manumitted no set aside bonus at the chief [url=http://www.baywatchcasino.com/]no put bonus
[/url].
Post a Comment