Java Facade Pattern
In this page:
What is the Facade Pattern?
The Facade pattern provides a single, simplified interface to a larger, more complex body of code, such as several interacting subsystem classes, without hiding the ability to use those subsystems directly if needed.
Example: What is the Facade Pattern?
public class Main {
static class CPU { void start() { System.out.println("CPU started"); } }
static class Memory { void load() { System.out.println("Memory loaded"); } }
static class ComputerFacade { // simplified interface to a complex subsystem
void start() { new CPU().start(); new Memory().load(); }
}
public static void main(String[] args) {
new ComputerFacade().start();
}
}
Login to try C/C++/Java/PHP code in the editor
Simplifying a Complex Subsystem
A facade is especially useful when a task requires coordinating several independent services or classes in a specific sequence, since it wraps that entire coordination logic behind one straightforward method.
Example: Simplifying a Complex Subsystem
public class Main {
static class InventoryService { void checkStock() { System.out.println("Stock checked"); } }
static class PaymentService { void charge() { System.out.println("Payment charged"); } }
static class ShippingService { void ship() { System.out.println("Order shipped"); } }
static class OrderFacade {
void placeOrder() { // coordinates the whole sequence in one method
new InventoryService().checkStock();
new PaymentService().charge();
new ShippingService().ship();
}
}
public static void main(String[] args) {
new OrderFacade().placeOrder();
}
}
Login to try C/C++/Java/PHP code in the editor
Facade Hides Subsystem Details
Because client code depends only on the facade's simple interface, the subsystem classes behind it can be refactored, replaced, or reorganized freely without breaking any code that only ever calls the facade.
Example: Facade Hides Subsystem Details
public class Main {
static class Subsystem { void internalWork() { System.out.println("Internal work done"); } }
static class Facade {
Subsystem subsystem = new Subsystem(); // could be swapped out freely
void perform() { subsystem.internalWork(); }
}
public static void main(String[] args) {
new Facade().perform(); // caller never touches Subsystem directly
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Facades for Different Clients
A system can expose multiple different facades over the same underlying subsystems, each tailored to a different kind of client, such as a simpler UserFacade and a more powerful AdminFacade.
Example: Multiple Facades for Different Clients
public class Main {
static class UserService { void viewProfile() { System.out.println("Viewing profile"); } }
static class AdminService { void deleteUser() { System.out.println("User deleted"); } }
static class UserFacade { void view() { new UserService().viewProfile(); } }
static class AdminFacade { void delete() { new AdminService().deleteUser(); } } // tailored to a different client
public static void main(String[] args) {
new UserFacade().view();
new AdminFacade().delete();
}
}
Login to try C/C++/Java/PHP code in the editor
Facade vs Direct Subsystem Access
Without a facade, calling code has to understand and correctly coordinate every subsystem class itself, while a facade collapses that responsibility into a single, well-tested entry point that's much simpler to use.
Example: Facade vs Direct Subsystem Access
public class Main {
static class ServiceA { void a() { System.out.println("A"); } }
static class ServiceB { void b() { System.out.println("B"); } }
public static void main(String[] args) {
new ServiceA().a(); // caller must know and coordinate every subsystem itself
new ServiceB().b();
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern