Notes
Outline
Chapter 8
Relations
This chapter discusses
Relations between cooperating objects.
Fundamental relationships that arise when we structure a system.
Relations between objects
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.
Person class
Remember this example?
This relation can be called the has-a-mother relation.
Person class (cont.)
Describing the relation more generically:
The arrow represents the source and target of the relation.
Inverse relation
This relation is bidirectional.
Many-to-one relation
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.
One-to-many relation
The inverse of a many-to-one relation.
is-mother-of  is a  one-to-many relationship.
One-to-many relation (cont.)
The asterisk represents a relation of zero or more objects.
Many-to-many relation
Maze relationship
Mathematical representations
A set of (source, target) pairs.
is-mother-of = {(MrsBennet,Elizabeth),
   (MrsBennet,Jane), … }
“Knowing” responsibility
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.
“Doing” responsibilities and the uses relation
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).
“Doing” responsibilities and the uses relation (cont.)
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.
Creation responsibility
The source object is responsible for creating the target object.
Implementing relations
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.
Object composition
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.
Object composition (cont.)
One-to-many and many-to-many relations
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.
One-to-many and many-to-many relations (cont.)
Establishing the relation
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.
Establishing the relation during creation
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
Establishing the relation during creation (cont.)
Assuming that mrsBennet is a Person object, we could create one daughter as follows:
Person elizabeth = new Person (…, mrsBennet, …);
Establishing the relation during creation (cont.)
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 ( … );
…
}
Establishing the relation during creation (cont.)
The class would include a component variable mySlider of type Slider.
private Slider mySlider;
…
Establishing the relation during execution
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;
}
Establishing the relation during execution (cont.)
 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.
Establishing the relation during execution (cont.)
 public void move (int direction) {
Room newRoom = room.connectedTo (direction);
room = newRoom;
}
“Uses” implemented with an argument
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
aggregation: the relation between one object and another, in which the second is an integral part of the first.
Properties of aggregation
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.
Properties of aggregation (cont.)
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;
  }
}
Properties of aggregation (cont.)
public class Car {
  public Car (Chassis c, Engine e, …){
  myChassis = c;
  myEngine = e;
  …
  }
private Chassis myChassis;
private Engine myEngine;
}
Properties of aggregation (cont.)
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.
Slide 36
Properties of aggregation (cont.)
Sometimes, parts are added to, or even removed from, the aggregate after it is created.
Hierarchical abstraction
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.
Hierarchical abstraction (cont.)
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.
Hierarchical abstraction (cont.)
Hierarchical abstraction (cont.)
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.
We’ve covered
Relations.
Multiplicity.
Knowing, doing, and creating responsibilities.
Creation using composition and object references.
Initializing a source object.
Aggregation.
Abstraction.
Glossary
Glossary (cont.)
Glossary (cont.)
Glossary (cont.)