Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 57 ยท Structural Patterns

Facade

Learn the Facade design pattern: hide a complicated subsystem behind one simple interface, with home-theater and checkout examples, and compare it with Adapter.

The Facade pattern puts one simple object in front of a group of complicated ones. Clients call a couple of friendly methods on the facade, and the facade works out which of the many subsystem classes to call, in what order, with what arguments.

You use facades all the time without naming them. A function like checkout(cart) that quietly validates stock, charges a card, creates an order and sends an email is a facade over four subsystems. A well-designed library entry point (requests.get(url), fetch(url)) is a facade over sockets, TLS, redirects, headers and decoding. The pattern's value is that most callers need the common path, not the full power of every part.

A front door for a busy building

Facade One small class with a few clear methods.
Subsystem Many classes that do the real work.
Client Talks to the facade, not to every part.

The subsystem classes stay public; the facade is a shortcut, not a wall.

1 / 2

The structure

There are only two roles. The facade knows which subsystem classes handle a request and delegates to them. The subsystem classes do the work and know nothing about the facade. Clients that need finer control can still use the subsystem classes directly.

classDiagram
    class Client
    class HomeTheaterFacade {
        +watchMovie(title)
        +endMovie()
    }
    class Amplifier
    class Projector
    class Lights
    class Player
    Client --> HomeTheaterFacade
    HomeTheaterFacade --> Amplifier
    HomeTheaterFacade --> Projector
    HomeTheaterFacade --> Lights
    HomeTheaterFacade --> Player

The client has one arrow in; the facade has four arrows out.

Movie night

Starting a movie with separate devices takes many steps, and doing them in the wrong order is a bug (the projector needs to warm up before the player starts, and the lights should dim first). The facade packages the sequence.

JS One call instead of seven

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The client wrote one line to start the movie and the facade performed seven device calls in the right order (and five more to shut everything down). If a new device joins the setup (a smart blind, say), only the facade changes.

A checkout facade

Facades also hide coordination: calling several services, handling their failures and keeping the outcome consistent. This version validates stock, charges, creates an order and releases the reserved stock if the payment fails.

JS A checkout that coordinates three services

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Bob's order fails at payment, and the facade releases the pens it had reserved, so the stock is back to 3 pens and no books. Without the facade, every caller would have to repeat that cleanup logic, and sooner or later one of them would forget it.

Facade versus Adapter

Both are wrappers, but they answer different questions:

  • Adapter makes an existing interface match another one a client already expects. It usually wraps one object and changes its shape, and it exists for compatibility. See Adapter.
  • Facade defines a new, simpler interface for a whole group of objects. It exists for convenience.

An adapter is asked "can this fit that socket?"; a facade is asked "can this be less work to use?" Facade is also related to Mediator: a mediator coordinates objects that talk to each other, while the objects behind a facade do not know the facade exists.

Guidelines and pitfalls

  • Keep the facade thin. It should orchestrate, not contain the business logic itself. If it grows complex rules, move them into subsystem classes or a service.
  • Do not hide the subsystem completely unless you must. Advanced callers may need the parts; a facade that blocks access forces them to work around it.
  • Beware of the god facade. One class that offers every operation of an entire application becomes a dumping ground. Make several smaller facades, one per use-case area.
  • Test the subsystems on their own, and test the facade for the sequence and the failure handling.
  • Layers. Facades work well at module boundaries: each package exposes a small facade, and other packages depend only on it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main purpose of a Facade?

    A facade offers a simplified entry point for common tasks so clients do not need to know or coordinate every subsystem class.
  2. 2. In the checkout example, why does the facade release reserved stock when payment fails?

    Coordinating several services and cleaning up after partial failure is part of the value of a facade, and it removes duplicated error handling from every caller.
  3. 3. Which statement best distinguishes Facade from Adapter?

    Adapter exists so that something fits an interface that already exists; Facade creates a new, simpler interface for a whole subsystem.
  4. 4. What is a common warning sign that a facade has gone wrong?

    A facade should stay thin and orchestrate. A huge class that holds the logic and every operation is a god object with a friendlier name.
  5. 5. Do the subsystem classes need to know about the facade?

    The dependency points one way: the facade uses the subsystem. This keeps the subsystem reusable and testable on its own.

Summary

  • A facade is a simple front door to a complex subsystem: few methods, common tasks, correct sequencing.
  • It lowers the learning curve, reduces coupling and lets internals change without affecting callers.
  • It is a good place to coordinate several services and handle partial failure.
  • Facade simplifies; Adapter makes things compatible; Mediator coordinates peers that know about it.
  • Keep it thin, keep the subsystem accessible, and split up a facade that turns into a god object.

Next lesson: Flyweight.