Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}