Facade: One Button, Many Parts

When every screen that takes an order has to run payment, stock, the receipt and the kitchen itself, in the right order, the steps get copied and one copy forgets a step; a facade puts one simple front on those parts, so every caller makes one call.

Apprentice · OOP · Updated

By

After this lesson you can

  • Put a simple front on a set of parts that must be used together in the right order
  • Spot the smells that point to Facade: the same steps, in the same order, copied into every caller
  • Tell Facade from Adapter and Mediator by what each one is for

Key takeaways

  • The smell: the same steps, in the same order, copied into every caller, and callers that have to know every part.
  • The cause: nobody owns the job "check out", so every caller does it themselves.
  • The fix: one simple front, checkout.placeOrder, runs the parts in the right order. The parts still exist; callers don't have to know them.
  • Facade, Adapter and Mediator all sit between pieces of code; they differ in what they're for.

Before this: Meet the Bakery App, and Why Singleton Is Usually a Trap (handing a class the objects it needs).

The owner's last wish this month: one button to check out. The bakery now takes orders two ways, at the counter tablet and on the phone, and checking out is four steps: take the payment, take the items off the shelf in the stock count, print the receipt, and tell the kitchen. Payment comes first, every time. Each screen does the four steps itself, and that's where things went wrong. The fix is a pattern with a French name, facade.

The symptom: the same steps, copied, and one copy forgot

Payments payments = new Payments();
Stock stock = new Stock();
Receipts receipts = new Receipts();
Kitchen kitchen = new Kitchen();

CounterTablet(3, 50);
PhoneOrder(3, 50);

void CounterTablet(int items, int total)
{
    Console.WriteLine("Counter tablet:");
    payments.Charge(total);
    stock.Take(items);
    receipts.Print(total);
    kitchen.Bake(items);
}

void PhoneOrder(int items, int total)
{
    Console.WriteLine("Phone order:");
    payments.Charge(total);
    receipts.Print(total);
    kitchen.Bake(items);
}

class Payments { public void Charge(int total) => Console.WriteLine(quot;Payment: ${total} charged"); }
class Stock { public void Take(int items) => Console.WriteLine(quot;Stock: {items} items taken off the shelf"); }
class Receipts { public void Print(int total) => Console.WriteLine(quot;Receipt: printed, total ${total}"); }
class Kitchen { public void Bake(int items) => Console.WriteLine(quot;Kitchen: {items} items to bake"); }
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments { void charge(int total) { IO.println("Payment: quot; + total + " charged"); } }
class Stock { void take(int items) { IO.println("Stock: " + items + " items taken off the shelf"); } }
class Receipts { void print(int total) { IO.println("Receipt: printed, total quot; + total); } }
class Kitchen { void bake(int items) { IO.println("Kitchen: " + items + " items to bake"); } }

Payments payments = new Payments();
Stock stock = new Stock();
Receipts receipts = new Receipts();
Kitchen kitchen = new Kitchen();

void counterTablet(int items, int total) {
    IO.println("Counter tablet:");
    payments.charge(total);
    stock.take(items);
    receipts.print(total);
    kitchen.bake(items);
}

void phoneOrder(int items, int total) {
    IO.println("Phone order:");
    payments.charge(total);
    receipts.print(total);
    kitchen.bake(items);
}

void main() {
    counterTablet(3, 50);
    phoneOrder(3, 50);
}
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments:
    def charge(self, total):
        print(f"Payment: ${total} charged")


class Stock:
    def take(self, items):
        print(f"Stock: {items} items taken off the shelf")


class Receipts:
    def print_receipt(self, total):
        print(f"Receipt: printed, total ${total}")


class Kitchen:
    def bake(self, items):
        print(f"Kitchen: {items} items to bake")


payments, stock, receipts, kitchen = Payments(), Stock(), Receipts(), Kitchen()


def counter_tablet(items, total):
    print("Counter tablet:")
    payments.charge(total)
    stock.take(items)
    receipts.print_receipt(total)
    kitchen.bake(items)


def phone_order(items, total):
    print("Phone order:")
    payments.charge(total)
    receipts.print_receipt(total)
    kitchen.bake(items)


counter_tablet(3, 50)
phone_order(3, 50)
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments { charge(total) { console.log(`Payment: ${total} charged`); } }
class Stock { take(items) { console.log(`Stock: ${items} items taken off the shelf`); } }
class Receipts { print(total) { console.log(`Receipt: printed, total ${total}`); } }
class Kitchen { bake(items) { console.log(`Kitchen: ${items} items to bake`); } }

const payments = new Payments();
const stock = new Stock();
const receipts = new Receipts();
const kitchen = new Kitchen();

function counterTablet(items, total) {
  console.log("Counter tablet:");
  payments.charge(total);
  stock.take(items);
  receipts.print(total);
  kitchen.bake(items);
}

function phoneOrder(items, total) {
  console.log("Phone order:");
  payments.charge(total);
  receipts.print(total);
  kitchen.bake(items);
}

counterTablet(3, 50);
phoneOrder(3, 50);
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments { charge(total: number): void { console.log(`Payment: ${total} charged`); } }
class Stock { take(items: number): void { console.log(`Stock: ${items} items taken off the shelf`); } }
class Receipts { print(total: number): void { console.log(`Receipt: printed, total ${total}`); } }
class Kitchen { bake(items: number): void { console.log(`Kitchen: ${items} items to bake`); } }

const payments = new Payments();
const stock = new Stock();
const receipts = new Receipts();
const kitchen = new Kitchen();

function counterTablet(items: number, total: number): void {
  console.log("Counter tablet:");
  payments.charge(total);
  stock.take(items);
  receipts.print(total);
  kitchen.bake(items);
}

function phoneOrder(items: number, total: number): void {
  console.log("Phone order:");
  payments.charge(total);
  receipts.print(total);
  kitchen.bake(items);
}

counterTablet(3, 50);
phoneOrder(3, 50);
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Receipt: printed, total $50
Kitchen: 3 items to bake

Look at the phone order: no stock line. Whoever wrote the phone screen copied three of the four steps, so phone orders never come off the stock count, and the bakery "has" cupcakes it sold yesterday. When a website comes along, it will copy the steps a third time.

  • The symptom: the same steps, in the same order, copied into every caller, and every caller has to know every part.
  • The cause: nobody owns the job "check out". Each screen does it itself, so each screen has to get it right.

What would you try?

A comment on each screen: "remember all four steps, payment first". Comments don't run, and the next screen's author may never see them.

Copy more carefully. The first copy was careful too. And when step five arrives (a loyalty-points update, say), every copy needs it.

Make each screen call the kitchen from inside the receipt code, and the receipt from the stock code. Now the parts are chained to each other, and none of them can be used on its own.

What's missing is one place whose whole job is "check out": it knows the parts and the order, and the screens ask it.

The fix: one simple front

A car's start button works this way. Pressing it runs a dozen parts: the fuel pump, the starter, the electronics, in exactly the right order. You don't need to know any of them. The parts are all still there for a mechanic, but the driver gets one button.

A car's dashboard with a single round button labeled "Start", and behind the dashboard, drawn as if seen through it, the parts it runs, each in a small labeled box: Fuel pump, Starter, Electronics, all connected to the button

In code, the button is one class, Checkout, with one method, placeOrder. It's handed the four parts (as in Why Singleton Is Usually a Trap) and runs them in the right order. Every screen makes one call:

A role diagram: "the counter tablet" and "the phone-order screen" each make one call to "checkout.placeOrder", the one simple front, which runs four parts in order, 1. payment, 2. stock, 3. receipt, 4. kitchen, inside a dashed fence labeled "the parts, in the right order"

Checkout checkout = new Checkout(new Payments(), new Stock(), new Receipts(), new Kitchen());

CounterTablet(3, 50);
PhoneOrder(3, 50);

void CounterTablet(int items, int total)
{
    Console.WriteLine("Counter tablet:");
    checkout.PlaceOrder(items, total);
}

void PhoneOrder(int items, int total)
{
    Console.WriteLine("Phone order:");
    checkout.PlaceOrder(items, total);
}

// One simple front: the steps, in the right order, in one place.
class Checkout
{
    private readonly Payments payments;
    private readonly Stock stock;
    private readonly Receipts receipts;
    private readonly Kitchen kitchen;

    public Checkout(Payments payments, Stock stock, Receipts receipts, Kitchen kitchen)
    {
        this.payments = payments;
        this.stock = stock;
        this.receipts = receipts;
        this.kitchen = kitchen;
    }

    public void PlaceOrder(int items, int total)
    {
        payments.Charge(total);
        stock.Take(items);
        receipts.Print(total);
        kitchen.Bake(items);
    }
}

class Payments { public void Charge(int total) => Console.WriteLine(quot;Payment: ${total} charged"); }
class Stock { public void Take(int items) => Console.WriteLine(quot;Stock: {items} items taken off the shelf"); }
class Receipts { public void Print(int total) => Console.WriteLine(quot;Receipt: printed, total ${total}"); }
class Kitchen { public void Bake(int items) => Console.WriteLine(quot;Kitchen: {items} items to bake"); }
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments { void charge(int total) { IO.println("Payment: quot; + total + " charged"); } }
class Stock { void take(int items) { IO.println("Stock: " + items + " items taken off the shelf"); } }
class Receipts { void print(int total) { IO.println("Receipt: printed, total quot; + total); } }
class Kitchen { void bake(int items) { IO.println("Kitchen: " + items + " items to bake"); } }

// One simple front: the steps, in the right order, in one place.
class Checkout {
    private final Payments payments;
    private final Stock stock;
    private final Receipts receipts;
    private final Kitchen kitchen;

    Checkout(Payments payments, Stock stock, Receipts receipts, Kitchen kitchen) {
        this.payments = payments;
        this.stock = stock;
        this.receipts = receipts;
        this.kitchen = kitchen;
    }

    void placeOrder(int items, int total) {
        payments.charge(total);
        stock.take(items);
        receipts.print(total);
        kitchen.bake(items);
    }
}

Checkout checkout = new Checkout(new Payments(), new Stock(), new Receipts(), new Kitchen());

void counterTablet(int items, int total) {
    IO.println("Counter tablet:");
    checkout.placeOrder(items, total);
}

void phoneOrder(int items, int total) {
    IO.println("Phone order:");
    checkout.placeOrder(items, total);
}

void main() {
    counterTablet(3, 50);
    phoneOrder(3, 50);
}
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments:
    def charge(self, total):
        print(f"Payment: ${total} charged")


class Stock:
    def take(self, items):
        print(f"Stock: {items} items taken off the shelf")


class Receipts:
    def print_receipt(self, total):
        print(f"Receipt: printed, total ${total}")


class Kitchen:
    def bake(self, items):
        print(f"Kitchen: {items} items to bake")


class Checkout:
    """One simple front: the steps, in the right order, in one place."""

    def __init__(self, payments, stock, receipts, kitchen):
        self.payments = payments
        self.stock = stock
        self.receipts = receipts
        self.kitchen = kitchen

    def place_order(self, items, total):
        self.payments.charge(total)
        self.stock.take(items)
        self.receipts.print_receipt(total)
        self.kitchen.bake(items)


checkout = Checkout(Payments(), Stock(), Receipts(), Kitchen())


def counter_tablet(items, total):
    print("Counter tablet:")
    checkout.place_order(items, total)


def phone_order(items, total):
    print("Phone order:")
    checkout.place_order(items, total)


counter_tablet(3, 50)
phone_order(3, 50)
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments { charge(total) { console.log(`Payment: ${total} charged`); } }
class Stock { take(items) { console.log(`Stock: ${items} items taken off the shelf`); } }
class Receipts { print(total) { console.log(`Receipt: printed, total ${total}`); } }
class Kitchen { bake(items) { console.log(`Kitchen: ${items} items to bake`); } }

// One simple front: the steps, in the right order, in one place.
class Checkout {
  #payments;
  #stock;
  #receipts;
  #kitchen;

  constructor(payments, stock, receipts, kitchen) {
    this.#payments = payments;
    this.#stock = stock;
    this.#receipts = receipts;
    this.#kitchen = kitchen;
  }

  placeOrder(items, total) {
    this.#payments.charge(total);
    this.#stock.take(items);
    this.#receipts.print(total);
    this.#kitchen.bake(items);
  }
}

const checkout = new Checkout(new Payments(), new Stock(), new Receipts(), new Kitchen());

function counterTablet(items, total) {
  console.log("Counter tablet:");
  checkout.placeOrder(items, total);
}

function phoneOrder(items, total) {
  console.log("Phone order:");
  checkout.placeOrder(items, total);
}

counterTablet(3, 50);
phoneOrder(3, 50);
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
class Payments { charge(total: number): void { console.log(`Payment: ${total} charged`); } }
class Stock { take(items: number): void { console.log(`Stock: ${items} items taken off the shelf`); } }
class Receipts { print(total: number): void { console.log(`Receipt: printed, total ${total}`); } }
class Kitchen { bake(items: number): void { console.log(`Kitchen: ${items} items to bake`); } }

// One simple front: the steps, in the right order, in one place.
class Checkout {
  private readonly payments: Payments;
  private readonly stock: Stock;
  private readonly receipts: Receipts;
  private readonly kitchen: Kitchen;

  constructor(payments: Payments, stock: Stock, receipts: Receipts, kitchen: Kitchen) {
    this.payments = payments;
    this.stock = stock;
    this.receipts = receipts;
    this.kitchen = kitchen;
  }

  placeOrder(items: number, total: number): void {
    this.payments.charge(total);
    this.stock.take(items);
    this.receipts.print(total);
    this.kitchen.bake(items);
  }
}

const checkout = new Checkout(new Payments(), new Stock(), new Receipts(), new Kitchen());

function counterTablet(items: number, total: number): void {
  console.log("Counter tablet:");
  checkout.placeOrder(items, total);
}

function phoneOrder(items: number, total: number): void {
  console.log("Phone order:");
  checkout.placeOrder(items, total);
}

counterTablet(3, 50);
phoneOrder(3, 50);
Counter tablet:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake
Phone order:
Payment: $50 charged
Stock: 3 items taken off the shelf
Receipt: printed, total $50
Kitchen: 3 items to bake

The phone order takes stock off the shelf now, and nobody went and fixed the phone screen: it stopped doing the steps itself. When the website arrives, it makes one call. When step five arrives, it goes in placeOrder, and every screen gets it.

The four parts are still there. The stock-count screen at closing time can still use Stock directly; a facade doesn't hide the parts, it means nobody has to know them.

How it got its name

A building's facade (from the French for "front", said fuh-SAHD) is the one face it shows the street, with all the rooms and pipes behind it. In code, a facade is one simple front on a set of parts that work together. This one has no modern, one-line version: a facade is already a class (or, in Python and JavaScript, a module of functions) with a few easy methods. It's in the original 1994 book as the Facade pattern.

Facade or its look-alikes?

Three patterns sit between pieces of code, and they're easy to mix up. What they're for is what tells them apart:

Pattern What it's for Strengths Weaknesses Pick it when
Facade One simple front on several parts that work together Callers make one call; the steps and their order live in one place It can grow into a class that does everything, if every new job is put in it Callers keep doing the same several steps with the same parts
Adapter Makes one thing look like a different shape that your code expects Code you can't change (another company's, say) fits without editing it One more layer to read; one adapter per thing that doesn't fit Something you need has the wrong shape (Pass 3)
Mediator One hub that parts talk through, so they don't talk to each other Parts stay independent; the rules about who reacts to what live in one place The hub can hold too many rules and become hard to follow Many parts need to react to each other (Pass 3)

A facade is called by your screens; the parts behind it never call it back. That's the quickest way to tell it from a mediator, which the parts themselves talk to.

Where you've already used it

  • File.ReadAllText("menu.txt") in C# and Files.readString(path) in Java open a file, read it, decode it and close it, behind one call.
  • fetch(url) in JavaScript and requests.get(url) in Python hide the network, the connection and the headers behind one call.
  • A framework's app.run() or builder.Build() starts dozens of parts in the right order.

When not to use it

  • When there's one part. A front for a single class is a renamed class.
  • When callers need fine control over each step (say, a refund that skips the kitchen). Give them the parts too, or a second method; don't make the facade guess.
  • When it starts collecting every job in the app. A facade for checking out shouldn't also handle refunds, reports and staff schedules. Then it's a "god class", which Pass 5 has a whole lesson about.

How often you'll meet it: every day (our estimate: almost every library you use has one, and so do most services in a web app).

Check yourself

2 questions to try on the lesson page.

So what

When every caller copies the same steps with the same parts, give the job one home with one simple front. The steps get done the same way everywhere, and the next step to add has one place to go.

Key terms

Cheat sheets