Facade Design Pattern in Java

Facade Design Pattern is behavioural design pattern. In this article we will look into the details of it and will discuss it with real time example to get clear picture is of its usage and applicability.

Facade Design Pattern
Facade Design Pattern

Introduction

Facade design pattern come under behavioural design pattern that provide simplified interface to access the complex functionality in the application.

Facade pattern provide a higher level of interface that makes an easy access to the subsystem in complex application.

When to Use the Facade Pattern

The Facade pattern is beneficial in scenarios where:

  • A system is very complex or difficult to understand.
  • An entry point is needed for each subsystem.
  • There is a need to layer your subsystems.

How to implement Facade Design Pattern

Let’s understand the facade design pattern by looking into the complex sub system inside online e commerce and see how the façade overcome complexity call with simpler approaches.

public interface OrderInventroy {
         private PlaceOrder placeOrder;// Individual class with funcanality
         private Payment payment;      // Payment gatways logic
         private Delivering delivering;// delivery details 
         //Constructor
         public void placeOrder(){  // Call this method from the facade call, so that it will delegate request the request to different class and response back to call with responce
               placeOrder = new PlaceOrder();
               payment = new Payment();
               delivering = new Delivering();
           }
}

Instead of calling the each class from the client program to perform any Order, we can delegate our request via facade design pattern in just single call i.e place order, simplest facade we can write like this.

public interface PlaceOrderFacade{
    void placeOrder();
}

Lets create a client program to demonstrator the working of facade design pattern.

package com.javaocean.facade;

public class ClientProgram{
public static void main (String[] args){
       PlaceOrderFacade facade = new PlaceOrderFacadeImpl();

       facade.placeOrder();// To place order we need to just call single method.
       System.out.println("Order placed.."); 
     }
}

Benefits of the Facade Pattern

Using the Facade design pattern offers several advantages:

  • Simplification: Facade pattern simplifies the interaction with complex systems by providing a single simplified interface with required functionality only.
  • Decoupling: It decouples the subsystems from the clients and other subsystems, promoting subsystem independence and portability.
  • Encapsulation: Facade hide the internal complexity involve withing subsystem and present required details only.
  • Manageability: Facade pattern provide facility to change and extend the underlying system and improves the readability and manageability of the code, enhancing the overall software maintainability.

Disadvantage of the Facade Pattern

  • Increased Complexity: Facade design pattern add layer extra abstraction level by introducing simplified interface, that could lead to potentially increasing in overall complexity of the system.
  • Increased Overhead: Adding an extra layer of indirection through the facade can introduce a slight performance overhead. Also, this can make the code harder to understand and debug, especially for developers unfamiliar with the pattern.
  • Reduced Flexibility: Facade design pattern acts as a single point of access to the underlying system. That could add limit to the flexibility for client, who want to access functionalities hidden within the subsystem.
  • Over-engineering: Some time applying the facade pattern to very simple systems can be overkill, adding unnecessary complexity where it’s not needed.

Few point about facade design pattern

There are a few benefits for the use of Facade pattern and a few points to be noted when Facade is to be approached.

  • Facade defines a higher-level interface that makes the subsystem easier to use by wrapping a complicated subsystem.
  • This reduces the learning curve necessary to successfully leverage the subsystem.
  • It also promotes decoupling the subsystem from its potentially many clients.
  • Facade can be applied at any point of development, generally we are placing this when no of interface get grow and system get complexes
  • Sub System are not aware of facade, they should not have any dependent or reference of facade interface.

 

You may find other post on different topic that might be helpful, Please have a look

  1. Factory design pattern in Java
  2. Builder design pattern
  3. Abstract class and Interface
  4. Java 8 new feature
  5. Observer Design Pattern in Java 8

Leave a Comment