Design Patterns Revisited - The Façade Pattern. Hiding the Complexity of the Complex..
Join the DZone community and get the full member experience.
Join For FreeIt is arguable whether the Façade pattern is a design pattern or not but it sure is a nice tool method for software developers. Since it occupies space both in GOF and Head First, I am also including it. The definition of Façade pattern according to mighty GOF is
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Seems easy isn't it, façade acts as a gateway for a series of complex procedures you need to do. All you have to do is delegate everything to your façade and use it as a gateway. If you need to call a specific method in the complex system, they are still available, just call them whenever you need.
public class Clothes{
private int amount;
private String owner;
private String status;
...
}
public class WashingPresets {
private WashingMachine washingMachine=new WashingMachine;
public Clothes lightWash(Clothes clothes) {
washingMachine.getWater(50);
washingMachine.getDetergent(20);
washingMachine.wash(clothes);
return clothes;
}
public Clothes heavyWash (Clothes clothes){
washingMachine.getWater(100);
washingMachine.getDetergent(35);
washingMachine.wash(clothes);
washingMachine.getWater(100);
washingMachine.getDetergent(40);
washingMachine.wash(clothes);
washingMachine.dry(clothes);
return clothes;
}
public Clothes normalWash(Clothes clothes) {
washingMachine.getWater(100);
washingMachine.getDetergent(40);
washingMachine.wash(clothes);
washingMachine.dry(clothes);
return clothes;
}
}
public class Client{
// anyone who wants to use our washing machine can just use such a simple code piece
public void washMyClothes(){
Clothes clothes=new Clothes();
WashingPresets ws=new WashingPresets();
ws.heavyWash(clothes);
}
}
public class TroublesomeClient{
public void washMyClothes(){
Clothes clothes=new Clothes();
WashingPresets ws=new WashingPresets();
WashingMachine washingMachine=new WashingMachine;
ws.lightWash(clothes); //façade call
washingMachine.getWater(50); //native methods call
washingMachine.wash(clothes); //native methods call
}
}
Talk only to your immediate friends!Always hide the complexity from the code which doesn't need to know the details.
Opinions expressed by DZone contributors are their own.
Comments