Facade Method Design Pattern in Python
Last Updated :
07 Aug, 2024
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 complexities of the underlying system, offering a simpler and more intuitive way to interact with it, enhancing maintainability and scalability.

Facade Method Design Pattern in Python
Important Topics for Facade Method Design Pattern in Python
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 the 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.
Components of Facade Method Design Pattern
The Facade Method Design Pattern consists of several key components that work together to simplify interactions with complex systems. Here are the primary components:
- Facade:
- Definition: A single class that provides a simplified interface to a complex subsystem.
- Role: The facade delegates client requests to the appropriate subsystem objects without exposing the underlying complexity.
- Subsystem Classes:
- Definition: The classes that perform the work of the system. These are the actual components that provide the functionalities.
- Role: These classes implement the subsystem functionalities. They are not directly accessible by the clients but are used internally by the facade.
- Client:
- Definition: The external entity that interacts with the subsystem through the facade.
- Role: The client makes requests to the facade, which in turn calls the appropriate subsystem methods.

Components of Facade Method Design Pattern
Facade Method Design Pattern example in Python
Problem Statement:
Suppose you are developing an e-commerce application where customers can place orders, make payments, and receive notifications. The process of handling an order involves multiple subsystems like inventory management, payment processing, and notification services. Directly interacting with these subsystems from the client code can make the application complex and difficult to maintain.
How Facade Method Design Pattern solves this problem?
Implement the Facade Method Design Pattern to simplify the interaction with these subsystems. The facade will provide a unified interface to the client, hiding the complexities of the underlying subsystems.

Facade Method Design Pattern example in Python
Below is the implementation of the above solution:
1. Subsystem Classes
1. Inventory Management
Python
class Inventory:
def check_stock(self, product_id):
print(f"Checking stock for product {product_id}")
# Simulate stock check
return True
def update_stock(self, product_id, quantity):
print(f"Updating stock for product {product_id} by {quantity}")
2. Payment Processing
Python
class Payment:
def process_payment(self, amount):
print(f"Processing payment of ${amount}")
# Simulate payment processing
return True
3. Notification Service
C++
class Notification:
def send_confirmation(self, order_id):
print(f"Sending confirmation for order {order_id}")
2. Facade Class
Python
class OrderFacade:
def __init__(self):
self.inventory = Inventory()
self.payment = Payment()
self.notification = Notification()
def place_order(self, product_id, quantity, amount):
if self.inventory.check_stock(product_id):
if self.payment.process_payment(amount):
self.inventory.update_stock(product_id, -quantity)
self.notification.send_confirmation(product_id)
print("Order placed successfully")
else:
print("Payment processing failed")
else:
print("Product is out of stock")
3. Client Code
Python
if __name__ == "__main__":
facade = OrderFacade()
facade.place_order(product_id=1, quantity=1, amount=100)
Below is the complete combined code:
Python
class Inventory:
def check_stock(self, product_id):
print(f"Checking stock for product {product_id}")
# Simulate stock check
return True
def update_stock(self, product_id, quantity):
print(f"Updating stock for product {product_id} by {quantity}")
class Payment:
def process_payment(self, amount):
print(f"Processing payment of ${amount}")
# Simulate payment processing
return True
class Notification:
def send_confirmation(self, order_id):
print(f"Sending confirmation for order {order_id}")
class OrderFacade:
def __init__(self):
self.inventory = Inventory()
self.payment = Payment()
self.notification = Notification()
def place_order(self, product_id, quantity, amount):
if self.inventory.check_stock(product_id):
if self.payment.process_payment(amount):
self.inventory.update_stock(product_id, -quantity)
self.notification.send_confirmation(product_id)
print("Order placed successfully")
else:
print("Payment processing failed")
else:
print("Product is out of stock")
if __name__ == "__main__":
facade = OrderFacade()
facade.place_order(product_id=1, quantity=1, amount=100)
OutputChecking stock for product 1
Processing payment of $100
Updating stock for product 1 by -1
Sending confirmation for order 1
Order placed successfully
Below is the explanation of the above code
- Subsystem Classes:
- Inventory: Manages stock checking and updating.
- Payment: Handles payment processing.
- Notification: Sends order confirmation notifications.
- Facade Class (OrderFacade):
- OrderFacade: Provides a simplified interface for placing orders by interacting with the
Inventory
, Payment
, and Notification
subsystems. - place_order: Method in
OrderFacade
that coordinates the process of placing an order. It checks the stock, processes the payment, updates the stock, and sends a notification.
- Client Code:
- The client creates an instance of
OrderFacade
and calls the place_order
method to place an order. - The client does not need to interact with the individual subsystems directly, simplifying the code and improving maintainability.
By using the Facade Method Design Pattern, the complexities of handling orders in an e-commerce system are hidden behind the OrderFacade
class, making the client code cleaner and easier to manage.
When to use Facade Method Design Pattern?
The Facade Method Design Pattern is used to simplify complex systems by providing a unified and easy-to-use interface. Here are specific scenarios where the Facade Method Design Pattern is beneficial:
- Simplify Complex Systems:
- When you have a complex system with multiple subsystems, the Facade provides a simple interface to interact with these subsystems.
- Example: In an e-commerce application, managing orders involves inventory, payment, and notification systems. The Facade simplifies these interactions.
- Promote Loose Coupling:
- To reduce dependencies between client code and complex subsystems.
- Example: In a multimedia application, various subsystems like video encoding, audio processing, and file handling can be accessed through a Facade, reducing direct dependencies on these subsystems.
- Hide Implementation Details:
- When you want to hide the complexities and implementation details of subsystems from the client.
- Example: In a home automation system, a Facade can hide the details of interacting with various devices like lights, thermostats, and security systems, providing a simple interface to control them.
- Improve Code Readability and Maintainability:
- To make the client code easier to read and maintain by reducing the clutter of interacting with multiple subsystems.
- Example: In a travel booking application, a Facade can simplify interactions with flight booking, hotel reservation, and car rental subsystems.
- Implementing Layers:
- To implement a well-defined interface for a layer in a layered architecture.
- Example: In a web application, a Facade can be used to provide a clean interface to the business logic layer, hiding the complexities of data access and processing layers.
- Managing Legacy Code:
- When integrating with legacy systems where you want to provide a simplified interface to the legacy code.
- Example: In a banking application, a Facade can provide a modern interface to interact with a legacy core banking system, hiding the old APIs and data formats.
When not to use Facade Method Design Pattern?
The Facade Method Design Pattern is highly useful in many scenarios, but there are situations where it might not be the best choice. Here are some situations when not to use the Facade Method Design Pattern:
- Unnecessary Abstraction:
- If the system is already simple and easy to understand, adding a Facade can introduce unnecessary abstraction and complexity.
- Example: A small application with straightforward interactions between components doesn’t need a Facade, as it may complicate an otherwise simple design.
- Direct Access to Subsystems Needed:
- When clients need to access the full functionality of subsystems directly, using a Facade can limit flexibility and control.
- Example: In a system where clients need to leverage the full range of capabilities of the subsystems, a Facade might hide important details and reduce the system’s usability.
- Performance Overhead:
- Introducing a Facade can add an extra layer of method calls, potentially leading to performance overhead in highly performance-sensitive applications.
- Example: In a real-time system where every millisecond counts, the additional method calls through a Facade might be detrimental.
- No Significant Complexity:
- When the subsystems are not complex enough to warrant a Facade, using one may not provide any tangible benefits.
- Example: If a system has only a few components with minimal interactions, adding a Facade won’t provide much simplification.
- Tight Coupling with Facade:
- If the Facade becomes tightly coupled with the client code, making changes to the subsystems may require changes to the Facade and the client, defeating the purpose of decoupling.
- Example: When the Facade becomes a bottleneck for changes and updates, it can hinder the system’s flexibility.