- What is It?
- Where and Why do We Use It?
- Key Components
- Principle Method
- Examples of Real-World Scenario
- Code without Pattern
- Code with Pattern
- Use cases of
- Advantages & Disadvantages
- Facade Vs Proxy
The Facade Design Pattern is used to provide a simple interface to a complex system. Instead of interacting with multiple classes and their methods, the Facade offers a unified interface to the client, simplifying their work and hiding system complexities.
-
Where:
- When a system has many complex classes and operations that the client needs to interact with.
- In layered architectures to manage communication between subsystems.
-
Why:
- To reduce the learning curve for users by hiding unnecessary details.
- To make code cleaner and more readable by creating a single entry point for clients.
- Facade: The interface that simplifies client interaction with the system.
- Subsystem Classes: The internal classes that perform the actual work.
- Client: The user or application that uses the Facade to interact with the system.
- The Facade acts as a middleman between the client and the subsystem.
- It ensures the client doesn't need to know the details of how the subsystems work.
- Clients communicate with the Facade, which delegates tasks to the subsystem classes.
-
Banking System: A customer interacts with a customer service executive (Facade) for tasks like opening an account or applying for a loan. Internally, the bank handles complex processes, such as account verification and document validation.
-
E-Commerce Checkout Process: When you click "Place Order," the system (Facade) internally manages inventory, payment processing, and delivery scheduling without requiring you to handle each step.
class Inventory {
public void checkStock() {
System.out.println("Stock is available.");
}
}
class Payment {
public void makePayment() {
System.out.println("Payment is successful.");
}
}
class Shipping {
public void arrangeShipping() {
System.out.println("Shipping is arranged.");
}
}
// Client - ecommerce customer
public class WthoutFacade{
public static void main(String[] args) {
Inventory inventory = new Inventory();
Payment payment = new Payment();
Shipping shipping = new Shipping();
inventory.checkStock();
payment.makePayment();
shipping.arrangeShipping();
}
}
class Inventory {
public void checkStock() {
System.out.println("Stock is available.");
}
}
class Payment {
public void makePayment() {
System.out.println("Payment is successful.");
}
}
class Shipping {
public void arrangeShipping() {
System.out.println("Shipping is arranged.");
}
}
// Facade
class ECommerceFacade {
private Inventory inventory;
private Payment payment;
private Shipping shipping;
public ECommerceFacade() {
this.inventory = new Inventory();
this.payment = new Payment();
this.shipping = new Shipping();
}
public void placeOrder() {
inventory.checkStock();
payment.makePayment();
shipping.arrangeShipping();
System.out.println("Order placed successfully!");
}
}
// Client
public class WithFacade{
public static void main(String[] args) {
ECommerceFacade ecommerce = new ECommerceFacade();
ecommerce.placeOrder(); // Placing the order
}
}
- Simplifying complex APIs, such as third-party libraries.
- Building user-friendly interfaces for applications with complicated backend operations.
- Managing subsystems in enterprise applications like ERP and CRM.
1. Simplifies client interaction by reducing system complexity.
2. Reduces dependency between the client and subsystem classes.
3. Enhances maintainability by decoupling subsystems from the client.
1. The Facade can become a "God Object" if it handles too much logic.
2. Overuse of the Facade may limit direct access to subsystem functionality when needed.
Hides access control, and clients know about the actual system but interact with the proxy to control or manage access.
Hides the complexity of subsystems, and clients don’t know about the internal subsystems; they interact only with the Facade for simplicity.