Facade pattern

On this page 7

The facade pattern is a structural design pattern that provides a single, simplified interface to a complex subsystem of classes, libraries or services. In software architecture, the facade sits between client code and the subsystem, accepts high-level requests such as placeOrder() and translates them into the many lower-level calls the subsystem actually needs.

The pattern was cataloged in 1994 in the book *Design Patterns* by the so-called Gang of Four, which defined its intent as making a subsystem easier to use. Its value lies in reducing coupling: clients depend on one stable entry point instead of on dozens of internal classes whose names, order of use and quirks may change.

How the facade pattern works

The facade pattern works by delegation. The facade class holds references to the subsystem components it needs and exposes a small set of methods that cover common use cases. Each facade method coordinates several subsystem calls in the correct order, passes data between them and returns a result in a form the client understands.

Importantly, the subsystem does not know about the facade. Its classes remain fully usable on their own, so advanced clients can bypass the facade when they need fine-grained control. The facade adds no new functionality of its own; it packages existing behavior behind a convenient boundary.

The participants of the pattern and their roles are summarized below.

ParticipantRoleKnows about
ClientRequests a high-level operationOnly the facade
FacadeDelegates and orchestrates callsSelected subsystem classes
Subsystem classesPerform the actual workEach other, not the facade
Additional facadeServes a different client groupAn overlapping subset of classes
Participants in the facade pattern

Typical uses of a facade in software architecture

Facades appear wherever a rich, low-level API meets client code that needs only a few operations. Classic examples include wrapping a media library behind convertVideo(file, format), hiding a payment provider’s multi-step authorization flow behind charge(order) or exposing a compiler’s lexer, parser and code generator through a single compile(source) call.

At the architectural level, the same idea shapes layered systems. An application service layer often acts as a facade over domain objects and repositories, and an API gateway in a microservice system plays a similar role for external clients, aggregating calls to several services into one request.

ScaleFacade exampleSubsystem hidden
Class levelReportExporter.export()Formatter, renderer, file writer
Library levelA simplified HTTP client wrapperConnection pools, retries, encoding
Module levelA billing module’s public serviceInvoices, taxes, ledgers
System levelAPI gateway or backend for frontendMultiple microservices
Facade applications at different scales

Facade compared with adapter, proxy and decorator

The facade is often confused with other wrapper patterns, because all of them place an object in front of other objects. The difference lies in intent and interface shape: a facade defines a new, simpler interface for many objects, while the others work with a single object and either keep or convert its interface.

PatternWrapsInterface offeredMain intent
FacadeA whole subsystemNew and simplifiedEase of use, lower coupling
Adapter patternOne objectConverted to an expected oneCompatibility
Proxy patternOne objectIdentical to the originalAccess control, laziness
Decorator patternOne objectIdentical, with extra behaviorAdd responsibilities
MediatorPeer objectsInternal to colleaguesCoordinate interactions
Facade and related structural patterns

The contrast with the mediator is subtle. A mediator centralizes communication among objects that know it and talk through it, whereas a facade is invisible to the subsystem and only simplifies access from the outside.

Benefits and trade-offs of the facade pattern

The main benefit of a facade is a narrower dependency surface. When a third-party library is upgraded or replaced, only the facade has to change, and the rest of the codebase stays untouched. Facades also make code easier to test, because a client can be tested against a mock facade instead of a full subsystem setup.

The trade-off is reduced flexibility. A facade intentionally hides options, so clients with unusual needs either bypass it or push for new facade methods. That pressure leads to the best-known failure mode: the facade gradually grows into a god object that knows everything and violates the single responsibility principle from SOLID principles.

SymptomLikely causeRemedy
Dozens of facade methodsOne facade serves every clientSplit into focused facades
Business rules inside the facadeLogic drifted out of the domainMove rules back to domain classes
Clients bypass it everywhereFacade is too restrictiveReview which use cases it covers
Leaky internal typesMethods return subsystem objectsReturn facade-owned data types
Facade over a single classUnneeded indirectionRemove it or use an adapter
Facade problems and remedies

Designing a good facade

A good facade is designed from the client’s point of view, not from the subsystem’s structure. Start with the tasks clients perform, name methods after those tasks and keep the number of methods small. Dependency injection works well here: the facade receives its subsystem components through its constructor, which keeps it replaceable and testable.

  • Keep the facade thin: it orchestrates, while rules stay in the subsystem or domain.
  • Return simple data types or facade-specific objects so internal classes do not leak.
  • Allow direct subsystem access for advanced cases instead of mirroring every option.
  • Define the facade behind an interface when clients should not depend on a concrete class.

A non-technical view

Think of a hotel concierge. A guest who wants dinner, a taxi and theater tickets could call the restaurant, the taxi company and the box office separately, learning each one’s rules. Instead, the guest makes one request to the concierge, who handles all the calls in the right order.

The facade pattern is that concierge for software. It gives the rest of a program one simple desk to ask for help, while the complicated work still happens behind it. The guest can still call the restaurant directly if needed, but most of the time the concierge is simpler and less error-prone.

A concrete example

An online retailer’s checkout code originally called seven subsystem classes directly: inventory reservation, price calculation, tax lookup, payment authorization, order persistence, shipping label creation and email confirmation. The same sequence was copied into the web shop, the mobile backend and an internal phone-order tool, and each copy handled failures slightly differently.

The team introduced a CheckoutFacade with two methods, placeOrder(cart, customer) and cancelOrder(orderId). The facade performs the calls in a fixed order and releases reserved stock when payment fails. When the retailer later switched payment providers, developers changed one class instead of three code paths, and the change shipped in a single short release cycle without touching any client application.