The Wayback Machine - https://web.archive.org/web/20240930202042/https://www.geeksforgeeks.org/facade-design-pattern-introduction/
Open In App

Facade Method Design Pattern

Last Updated : 18 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

facade-method-banner

1. What is the Facade Method Design Pattern?

Facade Method Design Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a high-level interface that makes the subsystem easier to use.

what-is-Facade-Method-Design-Pattern

In the above diagram,

  • Structuring a system into subsystems helps reduce complexity.
  • A common design goal is to minimize the communication and dependencies between subsystems.
  • One way to achieve this goal is to introduce a Facade object that provides a single simplified interface to the more general facilities of a subsystem.

2. When to use Facade Method Design Pattern

  • A Facade provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the facade.
  • There are many dependencies between clients and the implementation classes of an abstraction.
  • A Facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.
  • Facade define an entry point to each subsystem level. If subsystem are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.

3. Key Component of Facade Method Design Pattern

Component-of-Facade-Method-Design-Pattern

In the above diagram,

  • Consider for example a programming environment that gives applications access to its compiler subsystem.
    • This subsystem contains classes such as Scanner,Parser, ProgramNode, BytecodeStream, and ProgramNodeBuilder that implement the compiler.
  • Compiler class acts as a facade: It offers clients a single, simple interface to the compilersubsystem. It glues together the classes that implement compilerfunctionality without hiding themcompletely.
  • The compiler facade makes life easier for most programmers without hiding the lower-level functionality from the few that need it.

Facade (Compiler)

  • Facade knows which subsystem classes are responsible for a request.
  • It delegate client requests to appropriate subsystem objects.

Subsystem classes (Scanner, Parser, ProgramNode, etc.)

  • It implement subsystem functionality.
  • It handle work assigned by the Facade object.
  • It have no knowledge of the facade; that is, they keep no references to it.

Facade Method Design Pattern collaborate in different way

  • Client communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem objects.
  • The Facade may have to do work of its own to translate it inheritance to subsystem interface.
  • Clients that use the Facade don’t have to access its subsystem objects directly.

4. Problem Statement for the Facade Method Design Pattern

Problem-Statement-of-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.

5. Use Cases of Facade Method Design Pattern

  • Simplifying Complex External Systems:
    • A facade encapsulates database connection, query execution, and result processing, offering a clean interface to the application.
    • A facade simplifies the usage of external APIs by hiding the complexities of authentication, request formatting, and response parsing.
    • A facade can create a more user-friendly interface for complex or poorly documented libraries.
  • Layering Subsystems:
    • Decoupling subsystems: Facades define clear boundaries between subsystems, reducing dependencies and promoting modularity.
    • Providing high-level views: Facades offer simplified interfaces to lower-level subsystems, making them easier to understand and use.
  • Providing a Unified Interface to Diverse Systems:
    • Integrating multiple APIs: A facade can combine multiple APIs into a single interface, streamlining interactions and reducing code duplication.
    • Bridging legacy systems: A facade can create a modern interface for older, less accessible systems, facilitating their integration with newer components.
  • Protecting Clients from Unstable Systems:
    • Isolating clients from changes: Facades minimize the impact of changes to underlying systems by maintaining a stable interface.
    • Managing third-party dependencies: Facades can protect clients from changes or issues in external libraries or services.

6. Advantages of Facade Method Design Pattern

  • Simplified Interface:
    • Provides a clear and concise interface to a complex system, making it easier to understand and use.
    • Hides the internal details and intricacies of the system, reducing cognitive load for clients.
    • Promotes better code readability and maintainability.
  • Reduced Coupling:
    • Decouples clients from the underlying system, making them less dependent on its internal structure.
    • Promotes modularity and reusability of code components.
    • Facilitates independent development and testing of different parts of the system.
  • Encapsulation:
    • Encapsulates the complex interactions within a subsystem, protecting clients from changes in its implementation.
    • Allows for changes to the subsystem without affecting clients, as long as the facade interface remains stable.
  • Improved Maintainability:
    • Easier to change or extend the underlying system without affecting clients, as long as the facade interface remains consistent.
    • Allows for refactoring and optimization of the subsystem without impacting client code.

7. Disadvantages of Facade Method Design Pattern

  • Increased Complexity:
    • Introducing a facade layer adds an extra abstraction level, potentially increasing the overall complexity of the system.
    • This can make the code harder to understand and debug, especially for developers unfamiliar with the pattern.
  • Reduced Flexibility:
    • The facade acts as a single point of access to the underlying system.
    • This can limit the flexibility for clients who need to bypass the facade or access specific functionalities hidden within the subsystem.
  • Overengineering:
    • Applying the facade pattern to very simple systems can be overkill, adding unnecessary complexity where it’s not needed.
    • Consider the cost-benefit trade-off before implementing a facade for every situation.
  • Potential Performance Overhead:
    • Adding an extra layer of indirection through the facade can introduce a slight performance overhead, especially for frequently used operations.
    • This may not be significant for most applications, but it’s worth considering in performance-critical scenarios.

8. Conclusion

The facade pattern is appropriate when you have a complex system that you want to expose to clients in a simplified way, or you want to make an external communication layer over an existing system that is incompatible with the system. Facade deals with interfaces, not implementation. Its purpose is to hide internal complexity behind a single interface that appears simple on the outside.   

Other Articles:



Previous Article
Next Article

Similar Reads

Facade Design Pattern | JavaScript Design Pattern
Facade design pattern is a Structural design pattern that allows users to create a simple interface that hides the complex implementation details of the system making it easier to use. This pattern intends to create a simplified and unified interface for a set of interfaces hiding the implementation details making it less complex to use. Important
4 min read
Facade Method Design Pattern in Java
Facade Method Design Pattern is a structural design pattern that provides a simplified interface to a complex subsystem. It acts as a "front door," concealing the internal complexity of the subsystem and making it easier for clients to interact with it. In this article, we will get to know about what is Facade Method Design Pattern in Java, and why
9 min read
Facade Method Design Pattern in Python
The Facade Method Design Pattern in Python simplifies complex systems by providing a unified interface to a set of interfaces in a subsystem. This pattern helps in reducing the dependencies between clients and the intricate system, making the code more modular and easier to understand. By implementing the Facade pattern, developers can hide the com
7 min read
Facade Method - C++ Design Patterns
The Facade Pattern is a design pattern in software engineering that falls under the structural pattern category. It provides a simplified and unified interface to a set of interfaces or subsystems within a larger system, making it easier to use and reducing the complexity of the system for clients. Essentially, it acts as a facade or entry point to
10 min read
Difference Between the Facade, Proxy, Adapter, and Decorator Design Patterns
Understanding design patterns is crucial for software developers. Facade, Proxy, Adapter, and Decorator are key patterns that address different aspects of software design, from simplifying interfaces to enhancing functionality. This article explores their distinctions and practical applications in software development. Important Topics for Facade v
4 min read
Behavioral Design Pattern | JavaScript Design Pattern
Behavioral design patterns are a subset of design patterns in software engineering that deal with the interaction and responsibilities of objects. These patterns focus on how objects communicate and work together to achieve common tasks. Important Topics for the Behavioral Design Pattern in JavaScript Design Patterns Uses Cases of the Behavioral De
8 min read
Difference between Prototype Design Pattern and Flyweight Design Pattern
The major point in Prototype vs. Flyweight Design Pattern is that Prototype Design Pattern is a creational design pattern whereas Flyweight Design Pattern is a structural design pattern. In this post, we will look into this and more differences between the Prototype and Flyweight Design Patterns. Let us begin with a basic understanding of each of t
2 min read
Flyweight Design Pattern - JavaScript Design Pattern
The Flyweight Design Pattern is a structural design pattern used in JavaScript to minimize memory usage by sharing the data as much as possible with the related objects. Important Topics for Flyweight Design PatternDiagramatic Representation:Advantages of Flyweight design pattern:Disadvantages of Flyweight design pattern:The shared data typically c
4 min read
Mediator Design Pattern in JavaScript | Design Pattern
The Mediator pattern is a behavioral design pattern that promotes loose coupling between objects by centralizing communication between them. It's particularly useful when you have a complex system with multiple objects that need to interact and you want to avoid the tight coupling that can arise from direct object-to-object communication. Important
5 min read
Difference Between Builder Design Pattern and Factory Design Pattern
Design patterns provide proven solutions to common problems in software design. The Builder and Factory patterns are two popular creational design patterns. The Builder pattern constructs complex objects step by step. In contrast, the Factory pattern creates objects without specifying their exact class. Both patterns streamline object creation but
7 min read
Why is Singleton Design Pattern is Considered an Anti-pattern?
Let us explore why the Singleton pattern, a popular way to ensure only one instance of a class exists, is now seen as problematic in software development. In this article, we will discuss drawbacks such as tight coupling between components, difficulty in unit testing, and issues with scalability in larger applications. Important Topics for Singleto
8 min read
Strategy Method Design Pattern | C++ Design Patterns
Strategy Pattern is a behavioral design pattern that defines a family of interchangeable algorithms and allows them to be used interchangeably within a context. This pattern enables the algorithm to be selected at runtime, providing flexibility and promoting code reusability. Important Topics for the Strategy Method in C++ Design Patterns Example o
4 min read
State Method Design Pattern | C++ Design Patterns
In software design, managing the behavior of an object according to its internal state is a common issue. The state pattern addresses this issue by allowing an object to alter its behavior every time its internal state changes. This pattern encapsulates each state in a separate class, which makes it easier to add new states and modify existing stat
7 min read
Template Method Design Pattern | C++ Design Patterns
Template Method Pattern introduces a template in a superclass that defines the steps of an algorithm. These steps may include both common tasks shared among subclasses and specific tasks that need customization. Subclasses then implement or override these steps to modify the algorithm according to their specific needs. Important Topics for Template
7 min read
Composite Design Pattern | JavaScript Design Patterns
The Composite Design pattern is a way of organizing objects. It helps us handle different objects in a similar way when they are put together to create a structure with parts and wholes. These parts and wholes are like building blocks that can be split into smaller pieces and then put together to make a tree-like structure. The composite design pat
6 min read
Memento Design Pattern | C++ Design Patterns
Memento Design Pattern is a behavioral design pattern that provides a mechanism for capturing an object's internal state and restoring it to that state at a later time. This pattern is useful when we need to implement features like undo/redo functionality or when we want to save and restore an object's state for various reasons. Important Topics fo
7 min read
Design Patterns Cheat Sheet - When to Use Which Design Pattern?
In system design, selecting the right design pattern is related to choosing the right tool for the job. It's essential for crafting scalable, maintainable, and efficient systems. Yet, among a lot of options, the decision can be difficult. This Design Patterns Cheat Sheet serves as a guide, helping you on the path toward optimal design pattern selec
7 min read
Difference Between Bridge Pattern and Adapter Pattern
The Bridge Pattern and the Adapter Pattern are structural design patterns in object-oriented software development that primarily target the flexibility and reusability of a design. These two patterns have different roles and are used in different scenarios to solve specific design issues. Important Topics for Bridge Pattern vs Adapter Pattern What
5 min read
Difference between Strategy pattern and Command pattern
In software design, patterns provide reusable solutions to common problems. The Strategy and Command patterns are two important behavioral design patterns. The Strategy pattern allows the selection of an algorithm at runtime. The Command pattern encapsulates a request as an object. Understanding the differences between these patterns is crucial for
6 min read
Bridge Method | JavaScript Design Pattern
In software design, as systems grow and evolve, the complexity of their components can increase exponentially. This complexity often arises from the intertwining of different functionalities and features, making the system rigid, less maintainable, and harder to scale. The Bridge design pattern emerges as a solution to this problem, offering a way
9 min read
Memento Method - JavaScript Design Pattern
A behavioral design pattern in JavaScript called Memento focuses on externalizing and capturing an object's internal state so that it can later be restored. When you need to add features like undo/redo functionality, history tracking, or reverting an item to a former state, this pattern is quite helpful. Important Topics for the Memento MethodUnder
6 min read
Observer Method – JavaScript Design Pattern
Observer design pattern, a staple of behavioral design patterns in JavaScript, provides a means to establish a one-to-many relationship between objects. This design pattern is especially valuable for decoupling components and facilitating extensibility in software applications. By employing this pattern, you can have one object (the subject) notify
10 min read
Prototype Method - JavaScript Design Pattern
A Prototype Method is a JavaScript design pattern where objects share a common prototype object, which contains shared methods. The prototype method allows you to reuse the properties and methods of the prototype object, and also add new ones as needed. The prototype method is useful for performance optimization, as it avoids creating new functions
3 min read
Builder Method | JavaScript Design Pattern
The Builder design pattern is a creational design pattern used to construct complex objects by separating the construction process from the actual representation. It's especially useful when an object requires multiple steps or configurations to be created. Important Topics for the Builder Design Pattern in JavaScript Design Pattern Example without
9 min read
Strategy Method | JavaScript Design Pattern
Strategy Method or Strategy Pattern in JavaScript helps solve the problem of needing to use different methods or behaviors in your code and easily switch between them. Strategy Method is a behavioral design pattern in JavaScript that defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the client to choos
8 min read
Iterator Method | JavaScript Design Pattern
Iterator design pattern is a behavioral design pattern that provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It separates the responsibility of accessing and traversing the elements from the aggregate object. This pattern is widely used in many programming languages, including
4 min read
Builder, Fluent Builder, and Faceted Builder Method Design Pattern in Java
Builder Pattern is defined as a creational design pattern that is used to construct a complex object step by step. It separates the construction of an object from its representation, allowing us to create different variations of an object with the same construction code. This pattern is particularly useful when dealing with a complex object with ma
8 min read
Template Method Design Pattern in Java
Template Design Pattern or Template Method is the behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. This pattern falls under the category of the "behavioral" design patterns as it is concerned with how classes collaborate
10 min read
Composite Method | Software Design Pattern
Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. The main idea behind the Composite Pattern is to build a tree structure of objects, where individual objects and composite objects share a common interface. This common interface allows clients to interact wi
9 min read
Proxy Method Design Pattern in Java
A Proxy Method or Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. This pattern involves creating a new class, known as the proxy, which acts as an intermediary between a client and the real object. The proxy object controls access to the real object, allowing y
9 min read
Article Tags :