Damon Jasperson

Greenhorn
+ Follow
since Mar 19, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Damon Jasperson

QUOTE]That's not required for a Composition. It's true, though, that this is a quite strong indicator for a Composition relationship.

I disagree, here's why:

If composition implies lifecycle management by the composer (it does), then any outside object that can obtain a handle to the composed object (the Engine, in the example) can prevent garbage collection of an Engine that has been made available for garbage collection by virtue of its composer (the Car) being made available for garbage collection. In other words, by gaining access to the Engine explicitly, you've violated the contract of composition by subverting the lifecycle management defined by composition.

So, in this case, by destructing the car, you may not be able to destruct the engine because you've allowed outside access to the engine.
Composition can be easily identified in Java code. Remember that composition refers to the parts that make up the whole, so without the whole, there are no parts!

If a Car composes an Engine, then the following code will result:

public class Car {
private Engine engine;

public Car() {
engine = new Engine();
}

public void startEngine() {
engine.start();
}

protected void finalize() {
engine = null;
super.finalize();
}
}

The main thing to take away here is that there is no direct access to the Engine object. Ultimately, it should be understood that composition is a specialized form of aggregation (strong aggregation), which strictly defines the lifetime of a composed object.
[ March 19, 2008: Message edited by: Damon Jasperson ]