Toned-down Code Complexity Using Facade Design Pattern

Giving a “Face/Facade” makes the code simple and clear to understand.

Susith Hemathilaka
Javarevisited

--

Sportmode Facade DesignPattern CodeExample Java
Photo by Nathan Van Egmond on Unsplash

A facade is a class that provides a simple interface to a complex subsystem (large body of code) that contains lots of moving parts. It’s a structural design pattern and hides too much complexity to improve readability, understandability and maintenance.

Facade Class is working as structure and chain of the necessary methods into one caller method to decouple client implementation from sub-systems. It delegates most of the work to other classes. Usually, facades manage the full life cycle of objects they use. Key intentions of the design pattern is to hide much of the complexity and make the subsystem easy to use and decouple the client implementation from the complex subsystem.

When do we use facade?

1. When you need to have a straightforward interface to a complex subsystem.

ideal for very complex or difficult to understand due to the system having many interdependent classes.

further, subsystems get more complex over time. Even applying design patterns typically leads to creating more classes. A subsystem may become more flexible and easier to reuse in various contexts, but the amount of configuration and boilerplate code it demands from a client grows ever larger. The Facade attempts to fix this problem by providing a shortcut to the most-used features of the subsystem which fit most client requirements.

2. Use the Facade when you want to structure a subsystem into layers.

Create facades to define entry points to each level of a subsystem. You can reduce coupling between multiple subsystems by requiring them to communicate only through facades.

Real-world Analogy

Let’s say that we want to switch car driving mode manually. Then you have to perform the following operations in order to achieve the goal. The following diagram represents the process.

It can be quite complex and does require some knowledge to switching the modes correctly.

Most people like to do things with a single click. This is where user experience(UX) comes into play. We like to just press one button and don’t worry about the complexities. That’s the concept behind the facade design pattern.

Implementation

Step 1 : create the interface.

Step 2 : Create concrete classes implementing the same interface.

Step 3 : Create a facade class.

Step 4: Add the Driver Code.

Step 5: Verify the output.

Conclusion

Let’s retrospect to find out if the intentions behind the facade design pattern are satisfied through the above implementation. Sport mode and Comfort mode classes are used as subsystems to hide the complexity. DrivingModeFacade Class decouples the client and the client can switch driving mode using a single step.

The facade pattern doesn’t force us to unwanted tradeoffs, because it only adds additional layers of abstraction.

Sometimes the pattern can be overused in simple scenarios, which will lead to redundant implementations. Before Making it up decide the applicability by studying the objectives of a facade design pattern are satisfied.

Wrapping Things Up

Finally, I hope you got a clear idea of the facade design pattern and its use cases.

If there is anything that I have mentioned incorrectly, feel free to comment down below!. If you liked the post, click the 👏 below so more people can see it! Make sure that you follow me on Medium or on My Blog to get updates whenever a new article gets published.

Happy Coding!👌

--

--

Susith Hemathilaka
Javarevisited

An avid developer with an unquenchable thirst for anything in programming ✌ University of Westminster • Software Engineer @ DirectFN