Facade Design Pattern — An intuitive overview

Md Juhaer Adittya Pasha
5 min readNov 9, 2021

--

A Woven Facade of Metal Tubes. Photo by John Hill on https://www.world-architects.com/en/architecture-news/products/a-woven-facade-of-metal-tubes.

Introduction

In this article, I am going to discuss the facade design pattern. The facade design pattern is widely used in object-oriented programming. This design pattern helps you write cleaner code as well as be able to refactor them fairly easily, which makes them extremely useful for very large projects. Without further ado, let us consider an example.

Example

Imagine you have one client, that directly accesses three classes, namely P, Q, R. This is illustrated in the figure below.

Figure 1: One client interacting with three classes

From the above diagram, if we were to make changes to one or all the classes, namely, P, Q, and R, then this needs to be refactored on the side of the client as well. But then again, it's just one client, so it shouldn't be a big deal right? You would only need to change a few lines of code. Now let us look at the figure below.

Figure 2: Five clients interacting with the same three classes(It is worth noting that there should be arrows pointing from the client to the classes, but this is not provided since the diagram has too many lines)

Now, there are five clients. Still refactoring isn’t that difficult as you “only” need to manually refactor each of the five clients. Easy, but tedious. Now imagine if you had a hundred clients! Now it is certainly possible to manually refactor code in each of those hundred clients. But imagine a scenario where you’ve spent a good one hour of refactoring, you finally run the code and it gives the wrong result. Now it is up to you to go through each one of those hundred clients again and see where you made that mistake. Now again, with patience and hard work, you will certainly be able to find the issue. But there must be an easier way, right? Let us look at the figure below.

Figure 3: Three clients interacting with the three classes using the facade interface

Here we can see an intermediary called the facade. The facade can directly access the three classes. The clients access the facade. Therefore the facade here acts as an interface between the client(s) and the three classes. It hides much of the implementation details of the three classes. So, now when there are changes in the classes, they only need to be changed on the facade, and they would be automatically reflected on all the hundred clients as seen in our example above. Now if there are any errors, all you need to do is look at the facade, and you’ll find the error in no time. Now, this might sound overly simplistic, but this is just a brief introduction.

From our above example, we learned that the facade is effectively an interface between the client and the system, and deals with the complexities of the system by itself rather than the clients. Now let us look at some benefits and drawbacks of the facade design pattern.

Advantages

Below are some advantages of using the facade design pattern:

  1. Helps write cleaner code, since the client-side only needs to access the facade rather than all the methods of the various classes.
  2. Easier to refactor since we only need to change the facade rather than all the clients.
  3. Makes the software more flexible and expandable as it removes the need to refactor the client every time.
  4. Ensures less coupling.
  5. Minimizes the complexity of the subsystems.

Disadvantages

Now that we know the advantages, let us look at a few disadvantages of using the facade pattern:

  1. Could be difficult to add a facade to an existing code that already has a lot of classes and dependencies since a lot of refactoring is involved.
  2. Creates a high dependency on the facade interface. A problem in the facade would affect all the clients that use it.
  3. May not be very useful in simple scenarios, as was the case with only one client.

Implementation

Now below is a simple implementation of the facade. Do go through the code and see what is happening.

/*Sections of this code has been reused from: https://www.ionos.com/digitalguide/websites/web-development/whats-the-facade-pattern/
*/
public interface Shape {
void drawShape();
}
public class Rectangle implements Shape {
@Override
public void drawShape() {
System.out.println("This is a rectangle");
}
}
public class Square implements Shape {
@Override
public void drawShape() {
System.out.println("This is a square");
}
}
public class Circle implements Shape {
@Override
public void drawShape() {
System.out.println("This is a circle");
}
}
public class createShapeFacade{
private Shape circle;
private Shape rectangle;
private Shape square;
public createShapeFacade() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.drawShape();
}
public void drawRectangle(){
rectangle.drawShape();
}
public void drawSquare(){
square.drawShape();
}
}

If you’ve gone through the above code snippet, it should be obvious to you that the class createShapeFacade is acting as the facade, and clients would interact through it to create the three shapes, namely: rectangle, square, and circle.

Now, since this is just a brief overview, I am not diving into any more detail or providing any more implementations of a facade, but if you are curious to know more about how a facade is implemented, I would highly recommend checking out the following links:

  1. https://www.baeldung.com/java-facade-pattern
  2. https://www.tutorialspoint.com/design_pattern/facade_pattern.htm
  3. https://www.ionos.com/digitalguide/websites/web-development/whats-the-facade-pattern/

Conclusion

In summary, the facade design pattern is an excellent option to simplify certain interactions between various components in a complex program. Its ability to reduce coupling makes debugging and refactoring the program much easier. In the end, I hope you found this overview of the facade design pattern intuitive and are inspired to use them in your future projects.

References

  1. https://www.ionos.com/digitalguide/websites/web-development/whats-the-facade-pattern/
  2. https://en.wikipedia.org/wiki/Facade_pattern
  3. https://www.baeldung.com/java-facade-pattern
  4. https://www.tutorialspoint.com/design_pattern/facade_pattern.htm

--

--

Md Juhaer Adittya Pasha

Student at the School of Computing at the National University of Singapore