What is Facade Design Pattern?

Introduction to Facade Design Pattern

Facade design pattern is another pattern from structural design pattern category. This supports loose coupling by hiding the complex details via a simple interface. This pattern introduces a component called facade, which is a simplified interface component. This facade simplifies the responsibilities of the client by standing between the complex sub-systems. The complex subsystems can be a third party library, a legacy code, or lower level heavy code collections with a number of components with different modules. Usually, the sub-systems contain several complex processes, which require separate attention to executing.

Note to Remember

Does the ‘facade’ component in the pattern as same as the Java interface? No, it’s not the same concept. Facade doesn’t need exactly be an interface. It is just another layer to abstract the inner services and hides the complexity. Facade can be an abstract or concrete class. This facade sometimes known as a conceptual component, since we can to implement the additional layer in any form.

GoF definition

‘Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.’

This pattern removes a complexity of a code by introducing an additional layer over a set of complex components. It creates a simplified interface that performs many other actions behind the scenes. This higher-level unified interface covers the underlying structural complexity.

Simple Real Life Examples

We can find many instances of façade pattern application in our daily lives.

Retail Shops

Retail shops are a part of your life. When you want to buy something shops are everywhere. How façade relates to these? Let’s think you enter into a shop. You can select the item from the shelves or directly ask the salesperson. After you receive the item, you just have to pay the bill and take it home or use it right there. The cashier is the façade in this situation. Cashier act as the simplified interface that covers the complex process of selling an item on a shop shelve. Before it comes onto the rack shelve, it has to pass several processes within the retail shop.  First, that item is bought from a manufacturer. Then those are quality checked and entered into an inventory, then packed and labelled as required. Finally, distributed to the individual shops for selling. The customer only has to go to the nearest shop and order the item. The underlying complex process is hidden and done by another subsystem.

ATM Machine

Let’s assume a situation where a customer needs to withdraw money from a bank. When you insert the card at ATM the internal system performs several tasks and checks conditions before approving the withdrawal. Internal bank system checks whether the card is active and valid, then whether the account is valid, whether the entered security code is valid and match with the account holder and whether there is enough balance to release the money. The ATM will release money only after validating all the above tasks. Thus, there are many different processes behind the scene, in withdrawing money from an ATM.

Facade design pattern example

What is Façade?

The ‘Facade’ is a component, which represents an additional layer. It provides the main solution to the problem of dealing with complex sub-systems. This is a more simplified interface, which simply acts as the point of entry to the complicated subsystem.

In a more standard way, façade is a wrapper component that encapsulates a sub-system in order to hide the complexity of the subsystem. The ‘Wrapper’ contains set of members who access the subsystem on behalf of the façade user. It hides the inner processes and client only needs to communicate with façade in order to fulfil this requirement. In further complex systems, a façade class can be used to wrap all the interfaces and classes for a subsystem.

In real-world examples, waiter, salesperson or ATM machine interface act as the facades to hide complex internal processes.

How Does Façade Pattern work?

Façade pattern mainly consists of two sections. The external interface is known as the facade and the internal complex subsystem. When a client needs to get some service from the system, the external interface is the component, which directly interacts with the client. The client can communicate with the internal complex system only by sending requests to the façade. Façade will forward the requests to the suitable subsystem component. Façade will translate the client request as suited for the subsystem receiving interfaces. Subsystem objects will perform the actual work.

Hence, the client does not know the behaviours or the interactions of the internal subsystem. Façade pattern ‘information hiding’ design principle to prevent all client classes from seeing the internal class functionalities and object behaviours. However, the client can deal with the inner system when requires. The system will still expose the lower-level components for the clients who have any interest. Façade just provides an extra layer to cover the complex subsystem.

Example with Code

Let’s think about an online e-commerce store. It makes your shopping life easy by automating the item selection, order placing, and payment processes. E-commerce site handles various internal complex subsystems. However, it never burdens the customer with those internal processes. The customer only selects the item and do the payment. The selected item will be delivered within few days.

In the above scenario, e-commerce store act as an external simplified interface to connect the customer and the internal complex processes. Below are the components for façade pattern for this example.

Façade

Here the e-commerce site acts as the façade. It hides the complicated set of subsystems from the client.

Subsystems

Several subsystems will get together to fulfil the client order request until the completion. Below are identified main subsystems for the online e-commerce store. Each subsystem further consists of different complex processes before providing the final output.

  • Order Processing
  • Inventory Handling
  • Payment Processing
  • Shipment Processing

 

Steps to Implement

 

  1. Analyze the inner subsystems and identify a suitable unified façade.
  2. Design the ‘wrapper’ class that encapsulates the subsystem
  3. Identify the subsystem components – These could be another set of interfaces or direct classes
  4. Implement the identified subsystem components with the interface.
  5. Use the façade to access the subsystem

Identified Components to Implement the Pattern

  • Customer

The client program, which requests the service from the complex system. The customer selects an item from the e-commerce site and places an order. E-commerce site acts as the façade for the more complex underlying system and does the necessary handling to complete the placed order successfully.

  • OrderFacade

This is the main and simplified interface to connect to the external complex system.

  • Order Process Subsystem
    • Shopping Cart
    • Order
  • Inventory process Subsystem
    • Stock
    • Product
  • Payment Process Subsystem
    • Payment
  • Shipment Subsystem
    • Shipment
    • Shipment Provider

Class Diagram for Façade Design Pattern

Facade design pattern class diagram
Facade design pattern class diagram

Example Code Snippets

Subsystem Classes

Many internal subsystem classes work inter-dependently to fulfill the final customer request. Those classes are normal implementations to achieve some service. Hence, in this example we will be just mentioning the class name without providing any inner implementation.

Classes from Order Process Subsystem

ShoppingCart.java

Order.java

Classes from Inventory Subsystem

ShoppingCart.java

Order.java

Classes from Shipment Subsystem

ShoppingCart.java

Order.java

Classes from Payment Subsystem

ShoppingCart.java

Order.java

Following two classes shows how to implement the façade and client to receive the benefit of façade pattern.

OrderFacade.java

Customer.java

When to use Façade Pattern

  • system gets loaded and complex to handle by simple client calls
  • there is a need for abstraction to hide the internal complex procedures
  • classes of a system are tightly –coupled
  • there are many dependencies between system implementation and the client
  • there is heavy learning curve to understand the legacy system
  • there is a need for layering to the each subsystem

Advantage of Facade Pattern

  • It shields the client from dealing with the complicated inner system
  • Facilitate loose-coupling to reduce the complexity
  • Can use to shield the legacy and poorly designed code which is unable to refactor
  • Ability to introduce more the one façade to one system to layer the subsystems
  • Reduce the learning curve for new developers

Drawbacks of Façade Design Pattern

  • If used without a real need, your system may get further complicated by unnecessary codes
  • You have to keep a keen eye on modifications to the system. If any unmonitored modifications to the subsystem might break the façade execution

Leave a Reply