-J2EE Developer at Turkiye Is Bankası Core Banking Exchange project -Java-Flex Developer in TAV IT. Built softwares for several international airport (istanbul, ankara, izmir, tblisi, batumi, emfidha) with JPA/Spring/CXF/BlazeDS on server side and Flex on rich clients. -JavaEE&Process Project Laader at HSBC. -Presenter at Eclipsist 2008, on Rapid Rich Internet Application Development. Murat has posted 8 posts at DZone. View Full User Profile

Design Patterns Revisited - The Façade Pattern. Hiding the Complexity of the Complex..

08.01.2008
| 8633 views |
  • submit to reddit

It 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. 

One of the most challenging things people face when they move to away from their families is to do their own laundry. Even when you own a washing machine, there are many options, procedures and steps you need to complete. So what if we just design a new washing machine with preset programs like for "very dirty clothes; use double the amount of detergent, get water twice, wash for longer time then dry..etc" or for "very light dirt; use half the amount of detergent, get water once, wash for shorter, no need to dry..." or just a normal program like "get the detergent, get the water, wash, dry..". None of the steps above makes sense for me and I really don't care what the machine is doing as long as the output is clean clothes.  So lets just code a class which will be the façade of our washing machine and will only use the methods which were already provided with the washing machine.

 

    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);
}
}

We have just used the special methods provided by our high tech washing machine and hide the complexity in our façade class. So from now on our house mates just need to select the program which suites their laundry. So nice, so easy and so fast but what if one of our house mate is very ungenerous on spending money and doesn't want to use our heavy washing program because it uses to much detergent. Actually all he wants is to rinse the clothes once more without detergent and with less water and since he is the only one who wants to go to this we don't want to add this style as a method in our class. So we let him use our façade along with the methods provided by the producer;
    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
}
}

We didn't do too much but with this design we can hide the complexity and all clients including the ungenerous one is quite happy since everyone have much less things to do and it is hard to miss a step and ruin the laundry. As mentioned in Head First Design Patterns just try to obey the principle of least knowledge;
Talk only to your immediate friends!
Always hide the complexity from the code which doesn't need to know the details.
Published at DZone with permission of its author, Murat Yener.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Slava Imeshev replied on Sat, 2008/08/02 - 6:55pm

I think in case of the TroublesomeClient you have departed from the idea of Facade Pattern by letting leak the low-level details of the laundry.

A better implemenentation could be adding method lightWashAndRinse() the your client could call. So, for the TroublesomeClient it wold be

public class TroublesomeClient{  
public void washMyClothes(){
Clothes clothes=new Clothes();
WashingPresets ws=new WashingPresets();
ws.lightWashAndRinse(clothes);
}
}



Here the low level details are still hidden behind the facade and there is a reusable method that others could call.

Regards,

Slava Imeshev

Cacheonix: Data Grid for Java

Murat Yener replied on Mon, 2008/08/04 - 4:22am in response to: Slava Imeshev

Thanks for the example but actually I purposely leaked the facade code to show even such solutions may still be possible.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.