www wrote On 02/08/07 11:21,:
> For another better example to show my point:
>
> public class Car
> {
> final private Engine engine = new Engine(); //even the final word here
> may does not serve my purpose
>
> public void drive()
> {
> ...//code
> }
>
> public void stop()
> {
> ...//code
> }
>
> public void turnLeft()
> {
> ...//code
> }
>
> ...
>
> }
>
> I do not want Engine being completely immutable. I want to be able to
> tune or fix Engine sometimes by myself. But I do NOT want when Car is
> doing something(driving, stopping, or ...), Car is tuning or modifying
> Engine. That is dangerous!
public void tuneOrFix() {
if (engineIsRunning())
throw new IllegalStateException("pphhht!");
removeSparkPlugs();
...
}
You might also want to modify some of the other methods:
public void drive() {
if (maintenanceInProgress())
throw new IllegalStateException(
"not while it's on the lift!");
if (! engineIsRunning())
start();
...
}
--