Facade Design Pattern

The Facade Pattern is a structural design pattern that simplifies interactions between the client and a complex system by providing a single, easy-to-use interface.

When to Use Facade Pattern?

  1. When a system has too many complex subsystems that need a simplified interface.

  2. When you want to decouple clients from the system’s complexity.

  3. When multiple classes work together, but the client should only interact with one simple method.

Example:

Making Tea Without and With Facade

package DesignPatterns.structural.facade;

public class Water {
    public void boil() { System.out.println("Boiling water..."); }
}
package DesignPatterns.structural.facade;

public class TeaLeaves {
    public void add() { System.out.println("Adding tea leaves..."); }
}
package DesignPatterns.structural.facade;

public class Sugar {
    public void add() { System.out.println("Adding sugar..."); }
}
package DesignPatterns.structural.facade;

public class Milk {
    public void add() { System.out.println("Adding milk..."); }
}
package DesignPatterns.structural.facade;

public class TeaMakingWithoutFacade {
    public static void main(String[] args) { //client
        Water water = new Water();
        TeaLeaves teaLeaves = new TeaLeaves();
        Sugar sugar = new Sugar();
        Milk milk = new Milk();

        water.boil();
        teaLeaves.add();
        sugar.add();
        milk.add();
        System.out.println("Tea is ready...");
    }
}

Without the Facade Pattern, the client (user) has to manually call each step to make tea.

With Facade

package DesignPatterns.structural.facade;

class TeaFacade {
    Water water = new Water();
    TeaLeaves  teaLeaves = new TeaLeaves();
    Sugar sugar = new Sugar();
    Milk milk = new Milk();
    public void makeTea() {
        water.boil();
        teaLeaves.add();
        sugar.add();
        milk.add();
        System.out.println("Tea is ready...");
    }

}
public class TeaMakingWithFacade {
    public static void main(String[] args) {
        TeaFacade teaFacade = new TeaFacade();
        teaFacade.makeTea();

    }
}

Now, instead of calling each step manually, the Facade Pattern provides a simple interface (makeTea()).

Pros

  1. Simplifies Client Interaction

    Without Facade: The client must remember multiple steps – boiling water, adding tea leaves, sugar, and milk.
    With Facade: The client just calls makeTea(), and everything happens automatically.

  2. Hides Complexity of Subsystems

    Without Facade: The client must interact with Water, TeaLeaves, Sugar, and Milk separately.
    With Facade: These classes are hidden inside the TeaMaker facade.

  3. Reduces Code Duplication

    If multiple places in your application need to make tea, you don’t have to repeat the same steps everywhere.

    Without Facade: You write the same steps (boil water, add tea leaves, etc.) everywhere.
    With Facade: Just call makeTea(), and it works.

  4. Easier to Modify or Extend

    Suppose you want to add ginger to tea.

    Without Facade: You must update every place where tea is made.
    With Facade: Just update the TeaMaker class, and all clients get the update automatically.

  5. Improves Maintainability and Readability

    Instead of handling multiple classes, a developer only works with TeaMaker.
    Benefit: Easier debugging and better code readability.

Cons

  1. Less Flexibility for Clients

    Without Facade: The client has full control and can customize the tea-making process.
    With Facade: The client can only call makeTea(), which means no customization.

  2. Can Increase Code Size

    If a system is small and simple, adding a Facade class may not be necessary.

  3. Introduces an Extra Layer

    Sometimes, adding a Facade creates another layer of abstraction, which can slightly reduce performance.