Problem Statement for the Facade Method Design Pattern
Let’s consider a hotel. This hotel has a hotel keeper. There are a lot of restaurants inside the hotel e.g. Veg restaurants, Non-Veg restaurants, and Veg/Non Both restaurants. You, as a client want access to different menus of different restaurants. You do not know what are the different menus they have. You just have access to a hotel keeper who knows his hotel well. Whichever menu you want, you tell the hotel keeper and he takes it out of the respective restaurants and hands it over to you.
So here, Hotel-Keeper is Facade and respective Restaurants is system.
4.1 Step wise Step Implementation of above problem
Interface of Hotel
Java
package structural.facade; public interface Hotel { public Menus getMenus(); } |
The hotel interface only returns Menus. Similarly, the Restaurant are of three types and can implement the hotel interface. Let’s have a look at the code for one of the Restaurants.
NonVegRestaurant.java
Java
package structural.facade; public class NonVegRestaurant implements Hotel { public Menus getMenus() { NonVegMenu nv = new NonVegMenu(); return nv; } } |
VegRestaurant.java
Java
package structural.facade; public class VegRestaurant implements Hotel { public Menus getMenus() { VegMenu v = new VegMenu(); return v; } } |
VegNonBothRestaurant.java
Java
package structural.facade; public class VegNonBothRestaurant implements Hotel { public Menus getMenus() { Both b = new Both(); return b; } } |
Now let’s consider the facade,
HotelKeeper.java
Java
/*package whatever //do not write package name here */ package structural.facade; public interface HotelKeeper { public VegMenu getVegMenu(); public NonVegMenu getNonVegMenu(); public Both getVegNonMenu(); } |
HotelKeeperImplementation.java
Java
package structural.facade; public class HotelKeeperImplementation implements HotelKeeper { public VegMenu getVegMenu() { VegRestaurant v = new VegRestaurant(); VegMenu vegMenu = (VegMenu)v.getMenus(); return vegMenu; } public NonVegMenu getNonVegMenu() { NonVegRestaurant v = new NonVegRestaurant(); NonVegMenu NonvegMenu = (NonVegMenu)v.getMenus(); return NonvegMenu; } public Both getVegNonMenu() { VegNonBothRestaurant v = new VegNonBothRestaurant(); Both bothMenu = (Both)v.getMenus(); return bothMenu; } } |
From this, It is clear that the complex implementation will be done by HotelKeeper himself. The client will just access the HotelKeeper and ask for either Veg, NonVeg or VegNon Both Restaurant menu.
4.2 How will the client program access this façade?
Java
package structural.facade; public class Client { public static void main (String[] args) { HotelKeeper keeper = new HotelKeeperImplementation(); VegMenu v = keeper.getVegMenu(); NonVegMenu nv = keeper.getNonVegMenu(); Both = keeper.getVegNonMenu(); } } |
In this way, the implementation is sent to the façade. The client is given just one interface and can access only that. This hides all the complexities.
Facade Method Design Pattern
Facade Method Design Pattern is a part of the Gang of Four design patterns and it is categorized under Structural design patterns. Before we dive deep into the details of it, imagine a building, the facade is the outer wall that people see, but behind it is a complex network of wires, pipes, and other systems that make the building function. The facade pattern is like that outer wall. It hides the complexity of the underlying system and provides a simple interface that clients can use to interact with the system.
Important Topics for the Facade Method Design Pattern
- What is the Facade Method Design Pattern?
- When to use Facade Method Design Pattern
- Key Component of Facade Method Design Pattern
- Problem Statement for the Facade Method Design Pattern
- Use Cases of Facade Method Design Pattern
- Advantages of Facade Method Design Pattern
- Disadvantages of Facade Method Design Pattern