Facade Design Pattern

What is Facade Design Pattern?

  • In French, Facade means frontage. So whatever we see is facade. 
  • The primary motive behind Facade Design Pattern is a simple interface of a complex system. The system can be complex, but the interface has to be simple to operate the operations. 
  • The single class represents the entire system. Your system can contain an N number of subsystems, but the user would see it as a single system. 
  • The inner implementation should handle the entire complexity so the user should not be worried about that.  
  • It falls under Structural Design Pattern.

Table of Contents

Real Life Examples of Facade Design Pattern

Example 1:

ATM Sample PhotoWhen you are using an ATM, and you want to withdraw some amount, for instance, 10,000 Rupees. So, you need to insert your bank debit card in the ATM and enter the required password which is the passcode. Once you trigger the 10000 amount button, the following things will happen behind the screen.First, the machine will check if the account is valid or not. Then it will check whether you have sufficient funds to withdraw the entered amount. Then it’ll check whether the pin code is correct or not. After validating all these things, the amount will be withdrawn. This complicated procedure is handled behind the screen, but for the user, the interface is pretty simple.

Example 2:

building sample photoAnother example is a building that has ten floors with 2-3 rooms each. For a quality purpose, you’d need cement and bricks, etc. Here to build the structure you need to maintain many things which is a complex process.
You will give this task to the building contractor who will manage these things in a better manner. You’ll pay him 5,00,000 Rupees to that contractor, and you will explain him with your ideas. Then that contractor will handle these complicated things on behalf of us.

Facade Design Pattern Java Implementation Use Case

Example 1:

  • We would go to a hotel where Hotel Server handles our requests.
  • Here, Hotel Server handles all the complexities.
  • Example: If we ask for Veg menu the hotel server would go to the Veg Food Chef and ask the Veg Food Menu for that day and conveys that information to us.

UML Class Diagram of Facade Design Pattern

Facade Design Pattern Java Implementation

Step 1: Create an interface "Menu"

public interface Menu
{
    public void showMenu();
}

Step 2: Create a class "VegMenu" which implements Menu interface

public class VegMenu implements Menu
{

    @Override
    public void showMenu()
    {
        System.out.println("Idly");
        System.out.println("Dosa");
        System.out.println("Vada");
    }
    
}

Step 3: Create a class "NonVegMenu" which implements Menu interface

public class NonVegMenu implements Menu
{

    @Override
    public void showMenu()
    {
        System.out.println("Chicken Biriyani");
        System.out.println("Tandoori");
    }
    
}

Step 4: Create a class "HotelServiceStaff" which will be acting as a facade

public class HotleServiceStaff
{
    private VegMenu vegMenu;
    private NonVegMenu nonVegMenu;
    
    public HotleServiceStaff()
    {
        this.vegMenu=new VegMenu();
        this.nonVegMenu=new NonVegMenu();
    }
    
    public void showVegMenu()
    {
        this.vegMenu.showMenu();
    }
    
    public void showNonVegMenu()
    {
        this.nonVegMenu.showMenu();
    }
    
    public void showVegNonVegMenu()
    {
        this.vegMenu.showMenu();
        this.nonVegMenu.showMenu();
    }
    
}

Step 5: Create a demo class

public class FacadeEx
{
    public static void main(String[] args)
    {
        HotleServiceStaff hotleServiceStaff=new HotleServiceStaff();
        
        System.out.println("Veg Menu");
        hotleServiceStaff.showVegMenu();
        
        System.out.println("--------");
        System.out.println("Non Veg Menu");
        hotleServiceStaff.showNonVegMenu();
        
        System.out.println("--------");
        System.out.println("Veg & Non Veg Menu");
        hotleServiceStaff.showVegNonVegMenu();
    }
}
Share