Facade Design Pattern In Java

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

facade design pattern UML diagram

facade design pattern UML diagram

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:

Java


I have defined common operations like 'On' and 'Off' here.

Code for Fan class:

Java


I have added operations like 'Increase' and 'Decrease' speed here and its a sub-type of Appliance.

Code for Light class:

Java


No additional operations since we already have 'On' and 'Off' defined.

Code for SoundBar class:

Java


Here, I have added code for operations like 'Sound-Mode' and 'Increase', 'Decrease' , 'Setting' volume and 'Mute'.

Code for TV class:

Java


I have added operations like 'Increase/Decrease Volume', 'Increase/Decrease Channel'  and 'Sound Mute'.

Code for CoffeeMaker class:

Java


Code for ElectricGrill class:

Java


Here, I have added operation to set 'Temperature'.

Code for KitchenLight class:

Java


Code for Microwave class:

Java


Here, I have defined operations like 'Grilling On/Off', 'Pre-Heating' and 'Baking'.

Code for Refrigerator class:

Java


Here I have defined 'Mode' like 'Party' for fast cooling and 'Normal' for normal cooling.

Code for LivingRoomFan class:

Java


Code for LivingRoomFireTV4KStick class:

Java


Here, I have added operations like 'Open App', 'Close App', 'Search Content' and 'Play'.

Code for LivingRoomLight class:

Java


Here, I have defined operations to control light brightness by using 'dim' and 'bright'.

Code for LivingRoomSoundBar class: 

Java


Code for LivingRoomTV class:

Java


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:

Java


Here we have facade-methods to deal with 

Now, its time to write our Main application to execute our HomeFacade and test the output:

Java


Below is the output of the program:

Plain Text


That's it!

Source Code can be found here: Facade-Design-Pattern-Sample-Code

FAQs:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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

 

 

 

 

Top