Back to Design Pattern Course

Design Pattern Course

Facade is a Structural Design Pattern.

Facade provides a single, simplified interface to a set of interfaces in a subsystem. It hides the internal complexity of the subsystem from client code, so clients interact with one clean entry point rather than knowing about — and orchestrating — all the moving parts underneath.

The claim in the original that "practically every Abstract Factory is a type of Facade" is a simplification. They do share the idea of hiding complexity behind a unified interface, but they serve different purposes: Abstract Factory's purpose is to create families of related objects while keeping client code independent of concrete product types. Facade's purpose is to simplify access to a complex subsystem, with no constraint on object creation. Conflating the two tends to cause confusion in design discussions.

package com.praveen.designpatterns.structural.facade; public class Waiter { public void takeOrder() { System.out.println("Take Order from Client"); } public void takeOrderToCook() { System.out.println("Send Order to Cook"); } public void serveCustomer() { System.out.println("Serve Food to Client"); } }
package com.praveen.designpatterns.structural.facade; public class Kitchen { public void cookFood() { System.out.println("Cook the Food"); } public void signalReady() { System.out.println("Signal Food is Ready"); } }
package com.praveen.designpatterns.structural.facade; public class OrderFacade { private final Waiter waiter; private final Kitchen kitchen; public OrderFacade(Waiter waiter, Kitchen kitchen) { this.waiter = waiter; this.kitchen = kitchen; } public void orderFood() { waiter.takeOrder(); waiter.takeOrderToCook(); kitchen.cookFood(); kitchen.signalReady(); waiter.serveCustomer(); } }
package com.praveen.designpatterns.structural.facade; public class FacadePatternTest { public static void main(String[] args) { OrderFacade facade = new OrderFacade(new Waiter(), new Kitchen()); facade.orderFood(); } }

Output

Take Order from Client
Send Order to Cook
Cook the Food
Signal Food is Ready
Serve Food to Client

(Typo fixed from the original: serverCustomer() → serveCustomer().)

Two things worth noting about the example above: first, the Facade is receiving its dependencies via constructor injection rather than instantiating them internally — this is the correct approach, because it makes the Facade testable (you can inject mock Waiter and Kitchen objects in tests) and keeps it from being tightly coupled to the concrete subsystem classes. Second, Facade does not prevent clients from going around it and calling subsystem classes directly if they need to — it's a simplification layer, not an enforcement barrier. If you need to prevent direct access to subsystem classes, that's more of an access-control concern typically handled through package visibility or module boundaries (Java modules in Java 9+).

Facade is one of the most naturally occurring patterns in real enterprise codebases. Any Spring @Service that orchestrates multiple repositories, external API clients, and utility classes behind a single placeOrder(...) or processPayment(...) method is effectively a Facade. API gateway services that aggregate calls to multiple downstream microservices into a single response for a frontend client are Facades at the service/network layer.

Benefits

  • Shields clients from subsystem complexity — they deal with one simple interface rather than many.
  • Promotes loose coupling: changing the internal subsystem implementation doesn't affect clients as long as the facade's interface stays stable.
  • Makes the subsystem easier to use and reduces the learning curve for new team members.

Facade Pattern Examples in the JDK

  • javax.faces.context.ExternalContext — wraps ServletContext, HttpSession, HttpServletRequest, HttpServletResponse and other servlet-tier objects, presenting a single clean interface to JSF components so they don't need to know whether they're running in a Servlet or Portlet environment.
  • java.net.URL — provides a simple facade over the complex chain of URLConnection, URLStreamHandler, content handlers, and protocol details.
  • org.springframework.jdbc.core.JdbcTemplate — one of the most-used Facades in enterprise Java: it hides the complexity of JDBC (Connection management, PreparedStatement creation, ResultSet iteration, exception translation) behind a handful of simple query(), update(), and execute() methods.