Composition
Using instance variables that are references to other objects.
class Fruit {
class Fruit {
// Return int number of pieces of peel that
// resulted from the peeling activity.
public int peel() {
System.out.println("Peeling is appealing.");
return 1;
}
}
class Apple {
private Fruit fruit = new Fruit();
public int peel() {
return fruit.peel();
}
}
//...
}
class Apple {
private Fruit fruit = new Fruit();
//...
}
The composition relationship
12

13

Aggregation and Composition
-
Aggregation specifies a whole-part relationship between two objects.
- Composition is a stronger form of aggregation where the whole and parts have coincident
lifetimes, and it is very common for the whole to manage the lifecycle of its parts.
Examples of aggregation and composition.
14

Singletons
15

16

17

18
