0
0
LLDsystem_design~7 mins

Facade pattern in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a system has many complex classes and interfaces, users or other parts of the system struggle to interact with it easily. This complexity leads to errors, confusion, and slower development because clients must understand many details to use the system.
Solution
The Facade pattern provides a simple interface that wraps the complex parts of the system. It hides the internal complexity and offers a single, easy-to-use entry point. Clients interact with this facade instead of dealing with many classes directly, making the system easier to use and maintain.
Architecture
Client
Facade
Subsystem A

This diagram shows the Client interacting only with the Facade, which internally communicates with multiple complex subsystems.

Trade-offs
✓ Pros
Simplifies client interaction by hiding complex subsystem details.
Reduces dependencies between client code and subsystems, improving maintainability.
Improves code readability and usability by providing a clear, unified interface.
✗ Cons
Can become a god object if it tries to handle too many responsibilities.
Adds an extra layer, which might slightly impact performance in very high-throughput systems.
May hide useful subsystem features that clients might need, limiting flexibility.
Use when a system has multiple complex subsystems and clients need a simple interface to interact with them, especially when you want to reduce coupling and improve usability.
Avoid when the system is simple or when clients need full access to all subsystem features without restrictions.
Real World Examples
Amazon
Amazon uses facade-like APIs to provide simple interfaces for complex backend services like payment processing and inventory management, so frontend teams can integrate easily without knowing internal details.
Netflix
Netflix uses facade patterns in their client SDKs to hide complex streaming and recommendation subsystems behind simple APIs for app developers.
LinkedIn
LinkedIn applies facade patterns to unify access to multiple data sources and services, simplifying the client-side code for profile and feed features.
Code Example
The before code shows the client directly calling multiple subsystem methods, which is complex and error-prone. The after code introduces a Facade class that wraps subsystem calls into a single simple method. The client now calls only the Facade, reducing complexity and dependencies.
LLD
### Before applying Facade pattern (complex client code):

class SubsystemA:
    def operation_a1(self):
        return "SubsystemA: Operation A1"
    def operation_a2(self):
        return "SubsystemA: Operation A2"

class SubsystemB:
    def operation_b1(self):
        return "SubsystemB: Operation B1"
    def operation_b2(self):
        return "SubsystemB: Operation B2"

# Client must interact with both subsystems directly
sub_a = SubsystemA()
sub_b = SubsystemB()
result = []
result.append(sub_a.operation_a1())
result.append(sub_a.operation_a2())
result.append(sub_b.operation_b1())
result.append(sub_b.operation_b2())
print('\n'.join(result))

### After applying Facade pattern (simplified client code):

class Facade:
    def __init__(self):
        self.sub_a = SubsystemA()
        self.sub_b = SubsystemB()
    def simple_operation(self):
        results = []
        results.append(self.sub_a.operation_a1())
        results.append(self.sub_b.operation_b2())
        return '\n'.join(results)

# Client interacts only with Facade
facade = Facade()
print(facade.simple_operation())
OutputSuccess
Alternatives
Adapter pattern
Adapter changes an existing interface to a different one expected by clients, while Facade provides a new simplified interface over multiple interfaces.
Use when: Choose Adapter when you need to make incompatible interfaces work together without changing their code.
Proxy pattern
Proxy controls access to a single object, often adding security or caching, whereas Facade simplifies interaction with multiple objects.
Use when: Choose Proxy when you need to control or add behavior to access of a single object.
Summary
Facade pattern simplifies complex systems by providing a single easy-to-use interface.
It reduces dependencies and improves maintainability by hiding subsystem details.
Clients interact with the facade instead of many subsystems, making code cleaner and easier to use.