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?
When a system has too many complex subsystems that need a simplified interface.
When you want to decouple clients from the system’s complexity.
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
Simplifies Client Interaction
Without Facade: The client must remember multiple steps – boiling water, adding tea leaves, sugar, and milk.
With Facade: The client just callsmakeTea()
, and everything happens automatically.Hides Complexity of Subsystems
Without Facade: The client must interact with
Water
,TeaLeaves
,Sugar
, andMilk
separately.
With Facade: These classes are hidden inside theTeaMaker
facade.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 callmakeTea()
, and it works.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 theTeaMaker
class, and all clients get the update automatically.Improves Maintainability and Readability
Instead of handling multiple classes, a developer only works with
TeaMaker
.
Benefit: Easier debugging and better code readability.
Cons
Less Flexibility for Clients
Without Facade: The client has full control and can customize the tea-making process.
With Facade: The client can only callmakeTea()
, which means no customization.Can Increase Code Size
If a system is small and simple, adding a Facade class may not be necessary.
Introduces an Extra Layer
Sometimes, adding a Facade creates another layer of abstraction, which can slightly reduce performance.