Facade Design Pattern In Java
Join the DZone community and get the full member experience.Join For Free
Here I am with another article on design patterns — Facade Design Pattern. A Facade object is use to provide a simple interface by hiding complexities of a complex system.
Facade Design Pattern
- The Facade is a Structural Design Pattern and one of the Gang of Four design patterns.
- The Facade object is used to provide a front-facing interface by masking a more complex underlying system.
- The Facade may provide a limited or dedicated set of functionalities. But, the functionalities Facade provides are mainly required by the client application. So, its more caring as per client needs.
- The primary purpose of the Facade is to hide complexities of a system/subsystem by providing simpler interface to deal with.
- Using Facade is super-easy when we have to deal with a complex system/subsystem having lots of functionalities and different configurations.
- So, Facade hides minor and inner details of any third party library, system or subsystem we should know before we deal with it.
- In Java there are many features like JDBC, JPA, JAX-RS etc. which hides the minor details and provide a simpler interface in form of annotations or easier configuration to deal with.
- Even our computer system's POST (power-on-self-test) procedure which runs at the time we start our system; is a good example of Facade. it checks RAM, CPU, HDD and other connected peripherals before giving control over to operating system.
- Facade introduces additional layer of abstraction via Facade. So, if the sub-system changes, we need to do corresponding changes in the facade layer as well.
- We may also have multiple Facade objects one dealing with few subsystems and other dealing with some other subsystems.
I hope we are now clear about what is Facade? To understand it more clearly and the use of Facade in our code, let's take an example of Home Appliance we normally have in our home.
Home Appliance Application using Facade Design Pattern
For easier understanding of usage of Facade, I am here using the sample example application code what used in the Command Design Pattern. I only did some required changes and added the code for additional appliances and functionalities to make example more clear and interesting.
The example also help you to compare Facade with Command Design Pattern.
Code for Appliance class:
I have defined common operations like 'On' and 'Off' here.
Code for Fan class:
I have added operations like 'Increase' and 'Decrease' speed here and its a sub-type of Appliance.
Code for Light class:
No additional operations since we already have 'On' and 'Off' defined.
Code for SoundBar class:
Here, I have added code for operations like 'Sound-Mode' and 'Increase', 'Decrease' , 'Setting' volume and 'Mute'.
Code for TV class:
I have added operations like 'Increase/Decrease Volume', 'Increase/Decrease Channel' and 'Sound Mute'.
Code for CoffeeMaker class:
Code for ElectricGrill class:
Here, I have added operation to set 'Temperature'.
Code for KitchenLight class:
Code for Microwave class:
Here, I have defined operations like 'Grilling On/Off', 'Pre-Heating' and 'Baking'.
Code for Refrigerator class:
Here I have defined 'Mode' like 'Party' for fast cooling and 'Normal' for normal cooling.
Code for LivingRoomFan class:
Code for LivingRoomFireTV4KStick class:
Here, I have added operations like 'Open App', 'Close App', 'Search Content' and 'Play'.
Code for LivingRoomLight class:
Here, I have defined operations to control light brightness by using 'dim' and 'bright'.
Code for LivingRoomSoundBar class:
Code for LivingRoomTV class:
Now, when all the appliances are defined along with their operations, it's time to work on Facade Design Pattern. Suppose we like to a weekend-party at home with friends and family. Since we have various appliances at home for entertainment and food, we we write a HomeFacade to define our 'Week-End Home Party' operations.
Code for HomeFacade class:
Here we have facade-methods to deal with
- Setting up Home Entertainment System to play the movie.
- Preparing Food for family and friends.
- Shutting-down Home Entertainment System when the movie completes.
- Closing the Kitchen Appliances post our Home-Party.
Now, its time to write our Main application to execute our HomeFacade and test the output:
Below is the output of the program:
Source Code can be found here: Facade-Design-Pattern-Sample-Code
- Facade Pattern Vs Adapter Pattern
- Adapter Pattern allows to make incompatible system compatible. So, we fix the compatibility issue of the system with the client application. Without Adapter, we can't use the system (incompatible). Adapter generally works with one object. Read more on Adapter Design Pattern.
- Facade Pattern simplifies the complexity of the system (compatible but complex). Without Facade, we can still use the system. But it will require knowledge of lots of minor and inner details while we do that. Facade works with entire system.
- Facade Pattern Vs Command Pattern
- Facade Pattern hides internal details and provide a simplified interface.
- Command Pattern encapsulates actions which are required perform a task (undoable set of actions). Read more on Command Design Pattern.
- Facade Pattern Vs Mediator Pattern
- Facade Pattern defines the simplifies interface to a complex system.
- Mediator Pattern provides a central communication point between components of a system.
- Facade Pattern Vs Flyweight Pattern
- Flyweight Pattern creates smaller reusable objects for the system.
- Facade Pattern creates single bigger objects to deal with the entire system.
- Facade Pattern Vs Proxy Pattern
- Proxy Pattern is similar to Facade except, it provides same interface as it's service object to make complex objects interchangeable.
- Facade Pattern Vs Abstract Factory
- Abstract Factory is like Facade except it only handles the creation part of objects of the system/subsystem.
- Facade handles system's objects operational part as well.
- Facade Pattern Vs Singleton Pattern
- Facade Object normally we create as Singleton while implement since it serves for its purpose.
I hope this tutorial demonstrates the use of facade design pattern.
Liked the article? Please don't forget to press that like button. Happy coding!
Need more articles, please visit my profile: Brijesh Saxena
Opinions expressed by DZone contributors are their own.