Java Reference
In-Depth Information
LISTING 3‐1: Implementation of the washing machine analogy
public class WashingMachine {
public void heavilySoiled() {
setWaterTemperature(100);
setWashCycleDuration(90);
setSpinCycleDuration(10);
addDetergent();
addBleach();
addFabricSoftener();
heatWater();
startWash();
}
public void lightlySoiled() {
setWaterTemperature(40);
setWashCycleDuration(20);
setSpinCycleDuration(10);
addDetergent();
heatWater();
startWash();
}
}
// to use the façade
new WashingMachine().lightlySoiled();
If you want to use this functionality, just invoke the façade's lightlySoilded or heavilySoilded
method and let it do the complicated logic of washing the clothes. All complicated logic is kept
hidden by the façade and exposed via its two methods.
The implementation of the methods is decoupled from the client. This decoupling allows the
implementation to change without affecting any change in the way the client accesses the washing
services. The client knows nothing about the implementation of these methods, and it doesn't care.
All that it is interested in is obtaining the service it requires.
This example demonstrates one of many of the benei ts of the façade pattern. This topic does not go
into detail about the benei ts of the façade pattern, so what follows is a brief summary of the most
important ones:
A reduction in coupling because the client knows nothing about the subsystem
Increased maintainability and manageability when changes are required
Reuse of functionality because it encourages the reuse of controls and i ne‐grained logic
Consistency of service execution by invoking the same method consistently from one
invocation to the next
Reduction in business logic complexity by grouping related methods and invoking them from
one method invocation
Search WWH ::




Custom Search