Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Facade Pattern

What is the Facade Pattern?

The Facade Pattern is a structural design pattern that provides a unified, simplified interface to a set of interfaces in a subsystem. It hides the complexity of the subsystem and provides clients with a simple interface to interact with it.

Why is it Used?

  • Simplification: Hide subsystem complexity behind simple interface
  • Loose Coupling: Decouple clients from subsystem components
  • Easy to Use: Provide convenient methods for common operations
  • Maintenance: Changes to subsystem don't affect clients
  • Layering: Create logical layers in complex systems

Problems it Solves

1. Complex Subsystem Interactions

// WITHOUT Facade - Client must know all details
LightingSystem lights = new LightingSystem();
ACSystem ac = new ACSystem();
MusicSystem music = new MusicSystem();
SecuritySystem security = new SecuritySystem();

// Client must manage complex interactions
lights.turnOn();
ac.setTemperature(22);
music.play("Wake up song");
security.disarmAlarm();
// What if we add more systems? Code becomes messy!

// WITH Facade - Simple unified interface
SmartHome home = new SmartHome();
home.goodMorning(); // One call handles everything!

2. Learning Curve

New developers must understand entire subsystem to use it.

3. Scattered Logic

Subsystem interaction logic scattered throughout application.

SOLID Principles Included

1. Single Responsibility Principle (SRP)

  • Facade: Only provides simplified interface
  • Subsystems: Handle their specific responsibilities
  • Each component has clear purpose

2. Dependency Inversion Principle (DIP)

  • Client depends on Facade interface
  • Not on complex subsystem interfaces

3. Open/Closed Principle (OCP)

  • Facade open for extension (new methods)
  • Subsystems closed for modification

Example Without Facade Pattern

// Complex and error-prone - client must know everything
void orderFood() {
    // Kitchen operations
    Chef chef = new Chef();
    chef.prepareDish("Pizza");
    
    // Waiter operations
    Waiter waiter = new Waiter();
    waiter.takeOrder();
    
    // Cashier operations
    Cashier cashier = new Cashier();
    cashier.processPayment(amount);
    
    // Delivery operations
    DeliveryPerson delivery = new DeliveryPerson();
    delivery.deliverFood();
    
    // What if order of operations is wrong? Client must handle all details!
}

Example With Facade Pattern

// Simple and robust - facade handles complexity
void orderFood() {
    Restaurant restaurant = new Restaurant();
    restaurant.orderAndDeliver("Pizza", address); // One call!
}

// Facade internally handles all the complexity
public class Restaurant {
    void orderAndDeliver(String dish, String address) {
        chef.prepareDish(dish);
        waiter.takeOrder();
        cashier.processPayment();
        delivery.deliverFood();
    }
}

Key Characteristics

Simplified Interface: Provides easy-to-use interface ✓ Hides Complexity: Internal subsystem details hidden ✓ Grouping Operations: Related operations grouped together ✓ Decoupling: Clients independent from subsystem ✓ Layering: Creates architectural layers

Facade vs Adapter

Facade Adapter
Simplifies interface Makes incompatible interfaces work
Hides subsystem Wraps single object
Makes easy things easier Makes incompatible things compatible
Design-time decision Runtime decision

Facade vs Decorator

Facade Decorator
Simplifies complex subsystem Adds behavior to single object
One-to-many One-to-one
Hide subsystem Enhance single object
Architectural pattern Structural pattern

Real-World Examples

// Java's Collections framework
Collections.sort(list);           // Facade for sorting
Collections.unmodifiableList(l);  // Facade for protection

// JDBC - Database Facade
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);

// SLF4J - Logging Facade
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("Message");

// Spring's RestTemplate - HTTP Facade
restTemplate.getForObject(url, String.class);

Use Cases

  • Smart Home System: Single interface for lights, AC, security
  • Movie Booking System: Book tickets, select seats, process payment
  • Restaurant System: Order, prepare, deliver operations
  • Banking System: Deposit, withdraw, transfer operations
  • Car Dashboard: Engine, transmission, fuel displayed simply
  • Web Frameworks: Simple APIs hiding HTTP complexity
  • Database Access: ORM hiding SQL complexity

Real-World Analogy

Hotel Reception Desk (Facade):

  • Guest (Client) asks receptionist for everything
  • Receptionist (Facade) coordinates:
    • Room Service (subsystem)
    • Housekeeping (subsystem)
    • Front Desk (subsystem)
    • Maintenance (subsystem)
  • Guest doesn't need to know all departments
  • Receptionist provides simple, unified interface

Benefits

✓ Reduces complexity for clients ✓ Improves code readability ✓ Makes system easier to understand ✓ Loosens coupling with subsystems ✓ Makes changes to subsystem easier ✓ Provides single point of change

Common Mistake: Too Many Responsibilities

// Bad Facade - Too many methods
public class BadFacade {
    public void operation1() { }
    public void operation2() { }
    public void operation3() { }
    public void operation4() { }
    public void operation5() { }
    public void operation6() { }
    public void operation7() { }
    public void operation8() { }
    // Not really simplifying anymore!
}

// Good Facade - Related operations grouped
public class GoodFacade {
    public void goodMorning() { }     // Related operations
    public void leavingHome() { }     // Related operations
    public void comeBack() { }        // Related operations
}

Files in this Package

  • HomeAutomationFacade.java - Smart home with unified control interface
  • MovieTicketingFacade.java - Movie booking system facade