|
|
|
|
Relations between cooperating objects. |
|
Fundamental relationships that arise when we
structure a system. |
|
|
|
|
Objects interact to accomplish the goals of the
system of which they are a part. |
|
relation: a specific, well-defined association
between two or more objects. |
|
|
|
|
|
|
Remember this example? |
|
|
|
|
|
|
|
|
|
|
|
This relation can be called the has-a-mother
relation. |
|
|
|
|
|
|
|
|
Describing the relation more generically: |
|
|
|
|
|
The arrow represents the source and target of
the relation. |
|
|
|
|
|
|
This relation is bidirectional. |
|
|
|
|
|
|
A person can have the is-mother-of relation with
zero or more persons. |
|
A person can only have one mother. |
|
|
|
|
|
|
|
has-a-mother is a many-to-one relation. |
|
|
|
|
|
|
The inverse of a many-to-one relation. |
|
is-mother-of is a one-to-many
relationship. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The asterisk represents a relation of zero or
more objects. |
|
|
|
|
|
|
|
|
A set of (source, target) pairs. |
|
|
|
is-mother-of = {(MrsBennet,Elizabeth), |
|
(MrsBennet,Jane), … } |
|
|
|
|
If we require an object to know about another
object, then clearly the relation must be modeled. |
|
To do this, simply provide the source object
with a component variable that references the target. |
|
Example: an Explorer knows its location because
it is contained as a component variable. |
|
|
|
|
When carrying out some functional
responsibility, an object often needs the help of other objects. |
|
These collaborators are used by the original
object to assist in accomplishing its task. |
|
The original object (the client) invokes
features of the collaborator (the server). |
|
|
|
|
The relation is called uses: the client uses the
server. |
|
Example: When a Student enrolls in a Course, the
Student object requires that the Course object add the Student to the roll. |
|
|
|
|
The source object is responsible for creating
the target object. |
|
|
|
|
A fundamental method of implementing a relation
is simply to provide the source object with a reference to the target. |
|
This is an obvious approach for handling the
“knowing” responsibility. |
|
A “knowing” responsibility often translates into
a “uses” relation. |
|
|
|
|
composition: the process of constructing an
object using a component variable that references the other object. |
|
Using component variables to put objects
together is a fundamental mechanism of object design. |
|
We build an object by “collecting” auxiliary
objects that collaborate with the object and assist it in carrying out its
responsibilities. |
|
|
|
|
|
Relating to “many” objects is often done by
relating lists. |
|
A list is a particular form of a container. |
|
A container is an object whose purpose is to
“contain” a collection of other objects. |
|
|
|
|
|
|
|
|
The relation can be established |
|
when the source object is created, as part of
its construction, or |
|
later as a result of some action performed by
the source object. |
|
|
|
|
|
To establish a relation during creation: |
|
if the relation exists already, it is provided
as an argument to the source constructor. |
|
Example: |
|
public class Person { |
|
public Person (…, Person mother, …) { |
|
… |
|
this.mother = mother; |
|
… |
|
} |
|
… |
|
private Person mother;//This person’s mother |
|
… |
|
} // end of class Person |
|
|
|
|
Assuming that mrsBennet is a Person object, we
could create one daughter as follows: |
|
Person elizabeth = new Person (…, mrsBennet, …); |
|
|
|
|
If a target does not exist already, it can be
created in the source constructor. |
|
Example: Window class. The Window class creates a window on a computer screen. We want the window to have a slider
(scrollbar). |
|
|
|
public Window ( … ) { |
|
mySlider = new Slider ( … ); |
|
… |
|
} |
|
|
|
|
|
The class would include a component variable mySlider
of type Slider. |
|
private Slider mySlider; |
|
… |
|
|
|
|
Some source actions may result in establishing a
relation. |
|
Consider the move method in the Explorer class. |
|
|
|
public void move (rooms.Room newRoom){ |
|
room = newRoom; |
|
} |
|
|
|
|
A source
object can also be informed of a target as the result of a query. |
|
Consider that the move method is now given only
a direction to move. |
|
Assume that a room has an is-connected-to
relation with its neighboring rooms. |
|
|
|
|
public
void move (int direction) { |
|
Room newRoom = room.connectedTo
(direction); |
|
room = newRoom; |
|
} |
|
|
|
|
The client often need know about the server only
for the duration of a method execution. |
|
The Explorer need know about the Denizen while
the Explorer is executing its strike method. |
|
public void strike (denizens.Denizen
monster) { |
|
monster.takeHit(strengthPoints); |
|
} |
|
|
|
|
aggregation: the relation between one object and
another, in which the second is an integral part of the first. |
|
|
|
|
The parts are created when the aggregate is
created, and are “meaningful” only in their role of comprising the
aggregate; the aggregate and its parts have the same lifetime. |
|
Often the parts are created by the aggregate
constructor. |
|
|
|
|
Sometimes, other objects are responsible for
creating the parts and putting them together. |
|
Example: |
|
public class Assembler {… |
|
public
Car createCar () { |
|
Chassis theChassis; |
|
Engine theEngine; |
|
… |
|
theChassis =
cassisFactory.createChassis(); |
|
theEngine = |
|
engineFactory.createEngine(); |
|
… |
|
theCar = |
|
new Car(theChassis, theEngine,…) |
|
return
the Car; |
|
} |
|
} |
|
|
|
|
public class Car { |
|
public
Car (Chassis c, Engine e, …){ |
|
myChassis = c; |
|
myEngine = e; |
|
… |
|
} |
|
private Chassis myChassis; |
|
private Engine myEngine; |
|
} |
|
|
|
|
Clients typically are not given access to the
parts; requests go directly to the aggregate. |
|
By breaking the object down into coherent parts,
the structure of the object can be considerably simplified. |
|
|
|
|
|
Sometimes, parts are added to, or even removed
from, the aggregate after it is created. |
|
|
|
|
Abstraction is a key mechanism for managing
complexity in a system design. |
|
abstraction: the relation between a class and a
more specific, less abstract version of the class. |
|
Abstraction typically produces a hierarchy of
classes. |
|
|
|
|
|
|
The relation between a class and a more detailed
version is sometimes called generalization. |
|
The terms ancestor and descendant refer to generalization and extension
across one or more levels of abstraction. |
|
If A is a generalization of B and B is a
generalization of C, then both A and B are ancestors of C. |
|
If A is an extension of B and B is an extension
of C, then both A and B are descendants of C. |
|
|
|
|
|
|
|
Superclass refers to classes that are more abstract. Subclass refers to classes that are less
abstract. |
|
An extension
relation refers to a subclass extending a superclass. |
|
Java defines a standard class Object that is a
superclass of every other class. |
|
|
|
|
Relations. |
|
Multiplicity. |
|
Knowing, doing, and creating responsibilities. |
|
Creation using composition and object
references. |
|
Initializing a source object. |
|
Aggregation. |
|
Abstraction. |
|
|
|
|
|