Skip to content

Facade Pattern: One Simple Entry Point to a Messy Subsystem

Learn the Facade pattern through a hotel check-in example, with TypeScript, Python and PHP code, trade-offs and interview questions.

11 min readDesign patternStructural

Provide a single, simplified interface to a set of interfaces in a subsystem, so clients can perform common tasks without knowing how the subsystem's parts fit together.

Facade puts one small, task-shaped interface in front of a group of classes that are awkward to use together. Callers ask for the outcome they want, and the facade handles the call order, the glue data and the cleanup. In interviews, Facade comes up whenever you sketch a service layer, wrap a third-party SDK, or explain how you stopped three apps from copying the same 40 lines. This guide uses one scenario throughout: a hotel chain whose check-in flow is spread across four subsystems. You will see the problem, the structure, pseudocode, and the same runnable code in TypeScript, Python and PHP.

The problem

A situation where the obvious design starts to hurt.

Grovestay runs 14 hotels. Checking a guest in touches four internal modules, each written by a different team:

RoomInventory assigns a free room and can release it again. FolioService opens the guest's billing folio and places a deposit hold of $50 per night, and it can decline the hold. KeyCardEncoder writes a key card for a room and a number of nights. HousekeepingBoard marks the room as occupied so cleaners skip it.

The order matters. You must assign the room before you can open a folio against it, you must not encode a card if the deposit hold was declined, and if the hold fails you must release the room or it stays locked out of inventory until someone notices.

Three clients need check-in: the front-desk desktop app, the lobby self-service kiosk and the mobile app. Each team copied the sequence into its own code, about 40 lines each. The kiosk forgot the release step, so every declined card left a room stuck as assigned. When the folio team renamed a method, all three clients broke in the same release. Every client now depends on four modules, their method signatures and their failure rules, even though all any of them wants is "check this guest in for this many nights".

The solution

What the pattern proposes.

Create a CheckInDesk class that owns references to the four subsystem objects and exposes one method: checkIn(guest, nights). It returns a small result with the room number, card id and folio id.

Inside, checkIn runs the sequence once, in the correct order: assign a room, open the folio with a deposit of nights times 5,000 cents, release the room if the folio is declined, encode the card, mark the room occupied. The desktop app, the kiosk and the mobile app all call CheckInDesk.checkIn and nothing else.

The subsystem classes do not change and stay available. The housekeeping dashboard still talks to HousekeepingBoard directly, because it needs features the facade does not cover. The facade is a convenience for the common path, not a wall.

Now the release-on-failure rule lives in one place, a rename in FolioService touches one file, and each client depends on one class instead of four.

An everyday analogy

The same idea, outside software.

Think of a wedding planner. Behind one wedding are a venue, a caterer, a florist, a band and a photographer, each with their own contracts, deadlines and rules about who must be booked first. You could call all of them yourself and juggle the order. Or you tell the planner the date, the budget and the number of guests, and the planner handles the sequence, including cancelling the venue hold if the caterer falls through. You can still phone the florist directly if you want a specific bouquet; the planner just means you usually do not have to.

Structure

The pieces and how they relate.

Three clients call CheckInDesk, which coordinates four subsystem classes in the right order.FrontDeskApp calls CheckInDesk (checkIn). LobbyKiosk calls CheckInDesk (checkIn). MobileApp calls CheckInDesk (checkIn). CheckInDesk has a RoomInventory (1. assign / release). CheckInDesk has a FolioService (2. open folio). CheckInDesk has a KeyCardEncoder (3. encode card). CheckInDesk has a HousekeepingBoard (4. mark occupied).FrontDeskAppdesktop clientLobbyKioskself-service clientMobileAppguest phone clientCheckInDeskcheckIn(guest,nights)RoomInventoryassign() /release(room)FolioServiceopen(guest,depositCents)KeyCardEncoderencode(room, nights)HousekeepingBoardmarkOccupied(room)Subsystem stayspublicother tools maystill call i…checkIncheckIncheckIn1. assign / release2. open folio3. encode card4. mark occupied
Three clients call CheckInDesk, which coordinates four subsystem classes in the right order.

Participants

Facade (CheckInDesk)
Knows which subsystem objects handle which part of a request and in what order. Exposes task-level methods such as checkIn and translates between the client's simple inputs and the subsystem's detailed calls, including cleanup on failure.
Subsystem classes (RoomInventory, FolioService, KeyCardEncoder, HousekeepingBoard)
Do the real work. They have no reference to the facade and do not know it exists. They can still be used directly by code that needs more control.
Client (FrontDeskApp, LobbyKiosk, MobileApp)
Calls the facade instead of coordinating subsystem objects. Depends on one class and one method signature.
Additional facade (optional)
When one facade starts collecting unrelated tasks, split it, for example a CheckOutDesk alongside CheckInDesk. Facades can also use other facades.

Pseudocode

The solution to the scenario, language-neutral.

class CheckInDesk
    field rooms: RoomInventory
    field folios: FolioService
    field cards: KeyCardEncoder
    field housekeeping: HousekeepingBoard
    const DEPOSIT_PER_NIGHT_CENTS = 5000

    constructor(rooms, folios, cards, housekeeping)
        store all four references

    method checkIn(guest, nights): CheckInResult
        room = rooms.assign()
        try
            folioId = folios.open(guest, nights * DEPOSIT_PER_NIGHT_CENTS)
        catch error
            rooms.release(room)
            rethrow error
        cardId = cards.encode(room, nights)
        housekeeping.markOccupied(room)
        return CheckInResult(room, cardId, folioId)

// client code (kiosk, desktop app, mobile app)
desk = new CheckInDesk(new RoomInventory(), new FolioService(), new KeyCardEncoder(), new HousekeepingBoard())
result = desk.checkIn("Ana Ruiz", 3)
show result.room, result.cardId

The code

The same solution in TypeScript, Python and PHP.

class RoomInventory {
  private free: string[] = ["204", "305", "412"];

  assign(): string {
    const room = this.free.shift();
    if (room === undefined) {
      throw new Error("No rooms available");
    }
    return room;
  }

  release(room: string): void {
    this.free.unshift(room);
  }
}

class FolioService {
  private nextId = 100;

  open(guest: string, depositCents: number): string {
    if (depositCents > 100_000) {
      throw new Error("Deposit hold declined");
    }
    return `folio-${this.nextId++}`;
  }
}

class KeyCardEncoder {
  private nextId = 1;

  encode(room: string, nights: number): string {
    return `card-${this.nextId++}-room-${room}-${nights}n`;
  }
}

class HousekeepingBoard {
  private occupied = new Set<string>();

  markOccupied(room: string): void {
    this.occupied.add(room);
  }

  isOccupied(room: string): boolean {
    return this.occupied.has(room);
  }
}

interface CheckInResult {
  room: string;
  cardId: string;
  folioId: string;
}

class CheckInDesk {
  static readonly DEPOSIT_PER_NIGHT_CENTS = 5_000;

  constructor(
    private readonly rooms: RoomInventory,
    private readonly folios: FolioService,
    private readonly cards: KeyCardEncoder,
    private readonly housekeeping: HousekeepingBoard,
  ) {}

  checkIn(guest: string, nights: number): CheckInResult {
    const room = this.rooms.assign();
    let folioId: string;
    try {
      folioId = this.folios.open(guest, nights * CheckInDesk.DEPOSIT_PER_NIGHT_CENTS);
    } catch (err) {
      this.rooms.release(room);
      throw err;
    }
    const cardId = this.cards.encode(room, nights);
    this.housekeeping.markOccupied(room);
    return { room, cardId, folioId };
  }
}

const desk = new CheckInDesk(
  new RoomInventory(),
  new FolioService(),
  new KeyCardEncoder(),
  new HousekeepingBoard(),
);

const stays: Array<[string, number]> = [
  ["Ana Ruiz", 3],
  ["Ben Okafor", 30],
  ["Chen Wei", 2],
];

for (const [guest, nights] of stays) {
  try {
    const r = desk.checkIn(guest, nights);
    console.log(`${guest} -> room ${r.room}, ${r.folioId}, ${r.cardId}`);
  } catch (err) {
    console.log(`Check-in failed for ${guest}: ${(err as Error).message}`);
  }
}
// Ana Ruiz -> room 204, folio-100, card-1-room-204-3n
// Check-in failed for Ben Okafor: Deposit hold declined
// Chen Wei -> room 305, folio-101, card-2-room-305-2n

When to use it

Situations where it pays off.

  • Several clients repeat the same sequence of calls across a group of classes, and the sequence has ordering or cleanup rules.

    Copies drift. One client forgets the cleanup step, another misses a renamed method. A facade keeps the sequence in one place so a fix lands everywhere at once.

  • You wrap a large third-party SDK or legacy module, and your code only uses a small slice of it.

    The facade exposes just the operations you need in your own vocabulary. If you swap vendors or the SDK changes, you edit the facade, not every caller.

  • You want to layer a system, with each layer reaching the one below through a defined entry point.

    Facades at layer boundaries reduce the number of dependencies crossing the boundary, which makes the layers easier to change and test independently.

  • Newcomers or other teams need to use a subsystem without learning its internals first.

    A method named after the task, like checkIn, is easier to discover and harder to misuse than four classes with their own rules.

How to implement it

Steps to introduce it in existing code.

  1. Find the repeated workflow. Look for clients that call the same group of classes in the same order, and write down the task they are really trying to do in one sentence.

  2. Design the facade's method around that task, using the client's vocabulary and the minimum inputs. Return a small result object rather than leaking subsystem types where you can.

  3. Create the facade class and give it references to the subsystem objects, ideally passed in through the constructor so you can substitute fakes in tests.

  4. Move the call sequence, glue calculations and failure cleanup into the facade method. Keep business logic in the subsystem; the facade coordinates, it does not decide.

  5. Switch clients over one at a time and delete their copied sequences. Leave the subsystem public for code that needs advanced features.

  6. Watch the facade's size. When it gathers methods for unrelated tasks, split it into several focused facades.

Pros and cons

What you gain and what you pay.

Pros

  • Clients depend on one class with a small interface instead of several subsystem classes, so subsystem changes ripple less.
  • Ordering and cleanup rules live in one place, which removes a whole class of copy-paste bugs.
  • The common path becomes easy to read and easy to test with fakes for each subsystem object.
  • It does not hide the subsystem, so advanced callers keep full access.

Cons

  • A facade can grow into a god object that every part of the app depends on, if you keep adding unrelated methods to it.
  • It only covers the cases you designed for; clients that need slightly different behaviour either bypass it or push for more parameters and flags.
  • It adds a layer of indirection, which is wasted if only one client ever runs the sequence and it is short.

Relations with other guides

Patterns it's often confused or combined with.

Common questions

Short answers to the usual doubts.

What is the difference between Facade and Adapter?

Adapter makes an existing object fit an interface a client already expects; the interface is dictated from outside. Facade invents a new, simpler interface over several objects; you design it around the task. A quick test in an interview: if you are matching a required signature, it is an Adapter; if you are hiding a multi-step workflow, it is a Facade.

Should the facade hide the subsystem classes completely?

Usually no. The classic intent is to offer a simpler path, not to forbid the detailed one. In the Grovestay example the housekeeping dashboard still uses HousekeepingBoard directly. If you do want enforcement, use your language's module visibility, but accept that every new need will then have to go through the facade.

How is Facade different from Mediator?

With Mediator, the components hold a reference to the mediator and send their events through it, so communication is two-way and the components depend on it. With Facade, communication is one-way: the facade calls the subsystem, and the subsystem never calls back or knows the facade exists.

How do I stop a facade becoming a god object?

Keep each facade focused on one cohesive set of tasks and keep decisions in the subsystem. When you notice unrelated methods piling up, such as check-in, loyalty points and room service in one class, split them into CheckInDesk, LoyaltyDesk and so on. Constructor injection also helps, because a constructor with eight dependencies is a visible warning.

Is an API gateway or a service layer a Facade?

Conceptually, yes. A service-layer method like checkIn is a Facade over repositories and domain objects, and an API gateway exposing one endpoint that fans out to several backend services plays the same role at the network level. The trade-offs carry over too, including the risk of one entry point absorbing too much logic.

Next guide6 of 7 · Structural patternsFlyweight Pattern: Share Immutable State Across Many ObjectsRead the next one