Home  Java   Facade vs s ...

Facade vs Strategy design pattern differences

While Facade and Strategy patterns are both design patterns, they are not the same. Let’s break it down clearly.


1️⃣ Facade Pattern

Purpose

Key Idea

“Hide complexity behind a simple interface.”

Example

Imagine a home theater system:

class Amplifier { void on() {} void setVolume(int v) {} }
class DVDPlayer { void on() {} void play(String movie) {} }
class Projector { void on() {} void wideScreenMode() {} }

class HomeTheaterFacade {
    private Amplifier amp;
    private DVDPlayer dvd;
    private Projector projector;

    public HomeTheaterFacade(Amplifier a, DVDPlayer d, Projector p) {
        amp = a; dvd = d; projector = p;
    }

    public void watchMovie(String movie) {
        amp.on(); amp.setVolume(5);
        projector.on(); projector.wideScreenMode();
        dvd.on(); dvd.play(movie);
    }
}

Client code:

HomeTheaterFacade theater = new HomeTheaterFacade(amp, dvd, projector);
theater.watchMovie("Inception"); // one simple call

Key point: Client doesn’t need to know all the steps — Facade simplifies.


2️⃣ Strategy Pattern

Purpose

Key Idea

“Encapsulate interchangeable behavior and make it swappable.”

Example

Payment processing:

interface PaymentStrategy {
    void pay(int amount);
}

class CreditCardPayment implements PaymentStrategy {
    public void pay(int amount) { System.out.println("Paid " + amount + " with credit card"); }
}

class PayPalPayment implements PaymentStrategy {
    public void pay(int amount) { System.out.println("Paid " + amount + " via PayPal"); }
}

class ShoppingCart {
    private PaymentStrategy paymentStrategy;
    public ShoppingCart(PaymentStrategy ps) { paymentStrategy = ps; }
    public void checkout(int amount) { paymentStrategy.pay(amount); }
}

Client code:

ShoppingCart cart = new ShoppingCart(new PayPalPayment());
cart.checkout(100); // Pays via PayPal

Key point: You can switch the strategy (CreditCardPayment, PayPalPayment) at runtime.


3️⃣ Facade vs Strategy – Side by Side

FeatureFacadeStrategy
IntentSimplify interface to a subsystemEncapsulate interchangeable behaviors/algorithms
Client awarenessHides complexityChooses behavior dynamically
ExampleHomeTheaterFacadePaymentStrategy (PayPal/CreditCard)
Number of variationsUsually single facadeMultiple strategies
Change at runtimeNot neededYes, dynamic swapping
Published on: Oct 05, 2025, 11:22 PM  
 

Comments

Add your comment