What is Facade Pattern?
The Facade Pattern is a structural design pattern that provides a simple, unified interface to a complex system.
It hides internal complexity and exposes only what is needed.
Why Use Facade Pattern?
- Simplify complex workflows
- Reduce direct dependency on multiple classes
- Improve readability and usability
- Provide a clean API layer
Real-Time Scenario
In an Order System, placing an order involves:
- Creating order
- Processing payment
- Sending notification
👉 Instead of calling all services separately, use a facade.
Implementation
Step 1: Subsystems (Complex Services)
public class OrderService
{
public void CreateOrder() => Console.WriteLine("Order Created");
}
public class PaymentService
{
public void ProcessPayment() => Console.WriteLine("Payment Done");
}
public class NotificationService
{
public void Send() => Console.WriteLine("Notification Sent");
}
Step 2: Facade (Simplified Interface)
public class OrderFacade
{
private readonly OrderService _order = new();
private readonly PaymentService _payment = new();
private readonly NotificationService _notification = new();
public void PlaceOrder()
{
_order.CreateOrder();
_payment.ProcessPayment();
_notification.Send();
}
}
Usage Example
var facade = new OrderFacade();
facade.PlaceOrder();
Output
Order Created
Payment Done
Notification Sent
Key Concept
Instead of:
orderService.CreateOrder();
paymentService.ProcessPayment();
notificationService.Send();
We do:
facade.PlaceOrder();
👉 One method hides all complexity
Advantages
- Simplifies complex systems
- Reduces coupling between client and subsystems
- Improves code readability
- Easy to use and maintain
Disadvantages
- Can become a “god class” if overused
- May hide important system details
- Adds an extra layer
When to Use
- When system has multiple dependent services
- When you want a single entry point
- When simplifying API or service layer
- When working with complex workflows
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Order processing | Facade |
| Payment workflow | Facade |
| API orchestration | Facade |
| Microservice aggregation | Facade |
Pro Tip (Advanced .NET Usage)
- Use Facade in Application Layer (Service Layer)
- Combine with CQRS + Mediator for clean architecture
- Useful for building API endpoints that orchestrate multiple services
Summary
Facade Pattern helps you:
- Hide complexity
- Provide clean and simple APIs
- Improve maintainability
👉 Perfect for service orchestration, workflows, API layers
Comments
Post a Comment