Well, Façade is a design pattern that is being used by most of the programmers out there even without knowing.
Façade design pattern is similar to Adapter design pattern in a way, both acts as interfaces (middle layer). As we discussed earlier, Adaptor design pattern is a wrapper to avoid incompatibilities or to reduce complexities. E.g.: Java, c# Wrapper libraries for 3D programming or Image processing allows users to use defined functions than working with complex mathematics in these research areas.
Now let’s see how Façade hides some complexities (or just hiding large body of code) by introducing a middle layer.
Let’s take the scenario where you were assigned to create a Computer object.
After the completion you can ask the user to use the Computer. And he will first need to run powerUp() method to power up the system. Then, postMethod() which will run POST. Later he’ll need to run loadOS() method load the OS. Finally he will be able to start working.
class Computer {
public Computer() {
}
public void powerUP() { // ...
}
public void postMethod() { // ...
}
public void loadOS() { // ...
}
// ...
}
public class ExecComputer {
// User executes following ...
public static void main(String[] args) {
Computer computer = new Computer();
computer.powerUP();
computer.postMethod();
computer.loadOS();
// ...
}
}
I’m sure you notice the problem here. It’s not required for the user to run all these methods and it’s really a headache for users. (You can understand there are more methods to come like this)
So what’s the fix here? Yes you probably guessed correctly. We need to introduce a simple run() method which executes all these methods from the back in proper sequence. So user just have to remember the run() method.
Following code presents the fix;
class Computer {
private Computer computer;
public Computer() {
computer = new Computer();
}
private void powerUP() { // ...
}
private void postMethod() { // ...
}
private void loadOS() { // ...
}
public void run() {
computer.powerUP();
computer.postMethod();
computer.loadOS();
// ...
}
// ...
}
public class ExecComputer {
/**
* User executes following ...
*/
public static void main(String[] args) {
Computer computer = new Computer();
computer.run();
// ...
}
}
Well there you go. Simple as that.
I know what you think right now. Is there a design pattern for this? 🙂 Yes mate felt the same way when I first knew this. But don’t think Facade is all about this code, think about this concept and keep it in mind. You may miss this trick when you got your brain inside a massive code base. See you soon 🙂



































