Facade
- Is a Structural Design Pattern
- Facade provides a simple, unified interface to a complex subsystem
- It hides the complexity and gives you a single easy entry point
- Create one class that:
- Knows the subsystem
- Coordinates the calls
- Exposes a simple API
When to use?
- Subsystem is complex
- Client code is messy
- For loose coupling
- To expose a clean API
class CarFacade {
private Engine engine = new Engine();
private FuelInjector injector = new FuelInjector();
private AirFlowController airFlow = new AirFlowController();
void startCar() {
engine.start();
injector.inject();
airFlow.control();
System.out.println("Car started");
}
}
public class Client {
public static void main(String[] args) {
CarFacade car = new CarFacade();
car.startCar();
}
}