What is facade design pattern in Java? Facade Design Pattern is a commonly used software design pattern that is used to structure systems in a way that helps in reducing complexity. It is a straightforward pattern that is a part of the structural design pattern. Structural design patterns deal with class and object composition to form larger structures. It uses a basic pillar of object-oriented paradigm, inheritance, to compose implementations. Facade Design Pattern is no different and it shows a way to make single objects represent an entire subsystem. This design pattern exposes an object that carries out the responsibility of forwarding messages to the objects it represents.
Read: "Top 10 Backend Frameworks for Web Development in 2020"
The Facade design pattern is used in compilers where you can think of it as a large system of code that has other subsystems that have the functionality of parsing, scanning, tokenizing, creating bytecode streams, and whatnot. All of these functionalities can be represented by individual classes. Compliers have a unified interface that shields most of these classes and provides a single simple interface through which other subsystems can be communicated.
A real-world analogy for the Facade Design Pattern can be a Restaurant System which allows a customer to look at a menu and order the food he or she wants to have. Once the customer, looks at the menu and orders the food, what happens in the restaurant’s kitchen is not the concern of the customer. There might be other subsystems in the restaurant’s kitchen that do other tasks. The customer’s concern is to order the food from the menu and get it. The menu or the waiter depending on how the restaurant works can be thought of as the representation of the entire subsystem of the restaurant because the client can only directly interact with it. This way the complexities have been reduced.
Read: "The RoadMap for Java Developers in 2020"
If that did not make any sense to you, remember that you might already have come across this design pattern and only did not have a name for it. It will be clearer when we bring out the code examples. In this article we will discuss:
- What is facade design pattern in java?
- Facade design pattern participants.
- An example of Facade Design Pattern in java
- And some real-world analogies and examples where this design pattern may stick in.
What is Facade design pattern in java?
Before moving onto the “software engineering” side of the definition. Let us see, what is the literal meaning of the word facade? Facade means the exterior of a building that faces onto a street, a deceptive outward appearance. The exterior hides the structures inner functional components, thereby exposing just one thing to the people outside. The same applies to facade design pattern in software engineering. Facade design Pattern defines a higher-level interface that makes the subsystem easier to use. It provides a unified interface (facade) to a set of interfaces in a subsystem. The intent of this design pattern is to make a subsystem easier to use through a simplified interface.
One of the design principles that Facade Design Pattern leads to and we may adhere to it in complex scenarios is the Principle of Least Knowledge. This principle states that it is better to talk only to your “friends” and not with “friends of friends”. It means that when we are designing a system, we should be careful with the number of classes our objects interact with. The motive here is to create designs that prevent a large number of classes coupled together thereby preventing building a hard to maintain and fragile system.
Read: "What is Git and GitHub?"
But according to the facade design pattern, we should only have one unified interface that interacts with and that may cause one interface to get heavily coupled with other classes? In that case we can introduce additional facade classes to form layers of subsystems.
Let us look at a simple facade design pattern example in java that may refresh your memory of using it without actually knowing the pattern because it is that simple.
Facade Design Pattern Example in Java
We will try to mimic the Restaurant System Example, given earlier. Basically, we have a waiter class, a kitchen class and the facade class that acts as the unified interface. The food ordering process has been abstracted into the main class. The main purpose of this example is to demonstrate the use case of Facade Design Pattern. You will see how it helps in decoupling the overall system.
1. Waiter Class
Fig -: Facade design pattern example in java - Waiter Sub System
The Waiter class is the representation of what the waiter does in a restaurant. You can think of it as a subsystem in our version of the “Restaurant system”. The class exposes 3 methods that follow in order:
- takeOrder() – Take order from customer.
- takeOrderToCook() – Take the order from the client to the customer.
- serveCustomer() – Serve Prepared food to the customer.
2. Kitchen Class
Fig -: Facade design pattern example in java - Kitchen Sub System
The Kitchen is another subsystem in our representation of “Restaurant System”. The Kitchen class exposes two methods:
- cookFood() – Tells the cook to cook the food.
- signalReady() – The cook signals the waiter that the food is ready.
3. The Facade Class (OrderFacade)
Fig -: Facade design pattern example in java – Facade class (Order Facade)
Now, this is the class that acts as the unified interface for the entire restaurant system. The client, main class does not need to know anything about the overall process going on within the Restaurant. All it needs to know is that it can order food using the orderFood() method provided by the facade.
4. Ordering Food
Fig -: Facade design pattern example in java - Order Food
As you can see our client code only needs to call one method that takes care of the rest of the processes going on inside of the “system” instead of coupling itself with other classes and increasing dependencies that may lead to a design less maintainable. Any changes we make to the subsystems or add additional stuffs is pretty easy. It will not affect the client code. Our purpose of creating a loosely coupled system is achieved.
Read: "What is Mediator Design Pattern?"
Facade Design Pattern Participants
You might have guessed the key facade design pattern participants easily after having a glance at the code example. The facade design pattern participants are as follows:
- Facade – This is the main interface that knows which subsystem classes are responsible for a request from the client.
- Subsystem Classes – These classes handle the work assigned by the facade class. They keep no reference of the facade class i.e. are completely independent of it.
When to use facade design pattern?
So, what was the point of creating a separate interface that delegated requests to responsible sub classes? Let us see what are the consequences of using it:
- The client code is shielded from subsystem classes, as we saw in the Restaurant system example. This way we can reduce the number of objects the client deals with directly.
- The Facade design pattern promotes loose coupling by eliminating complex and circular dependencies.
- Client code that uses facade design pattern do not have to access its subsystems directly thereby, promoting clean code.
Read: "Top Best Tools For Java Developers In 2020"
Conclusion:
Generally, when we use inheritance, we create a parent class which is then extended by the child classes. The functionalities are then inherited by each child class. The Facade design pattern is similar but with a slight variation. In the case of inheritance, the child classes may be highly coupled with the parent and making changes to the parent may affect the child classes as well. However, as you might have noticed using the facade design pattern, we can make changes to the facade class, the subsystems without facing major issues due to decoupling.