← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 11 of 14

Java Facade Pattern

The Facade pattern provides a single, simplified interface to a larger, more complex body of code, hiding the coordination of several interacting subsystem classes behind one entry point.

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?

java
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();
	}
}

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

java
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();
	}
}

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

java
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
	}
}

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

java
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();
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.