Facade

Learning Objectives

Provide a simple interface to a complex subsystem.

Keep facades thin; avoid god services.

Prerequisites: Module 21.

Intuition and Definition

Intent: provide a unified, higher-level interface to a set of interfaces in a subsystem, making it easier to use.

Participants: Facade (knows which subsystem classes to call and in what order) and the subsystem classes (unaware of the facade; still usable directly).

Why It Matters

Clients that need a common task ("flash new firmware", "place an order") shouldn't need to know ten subsystem classes and the order to call them. Facades reduce coupling to the subsystem's internals.

Syntax and Language Notes

Facades are ordinary classes or module-level functions; no special language support is needed.

Complete Example

#include <iostream>
#include <string>
#include <vector>

// subsystem classes (each useful on its own)
class DiagnosticSession { public: bool enterProgramming() { std::cout << "  session: programming mode\n"; return true; } void exit() { std::cout << "  session: default mode\n"; } };
class SecurityAccess { public: bool unlock(int seed) { std::cout << "  security: key for seed " << seed << " accepted\n"; return true; } };
class FlashMemory {
public:
    bool erase() { std::cout << "  flash: erased\n"; return true; }
    bool write(const std::vector<int>& image) { std::cout << "  flash: wrote " << image.size() << " blocks\n"; return true; }
    bool verify(int crc) { std::cout << "  flash: crc 0x" << std::hex << crc << std::dec << " ok\n"; return true; }
};
class EcuReset { public: void hard() { std::cout << "  ecu: hard reset\n"; } };

// facade: one call for the common task, correct order and cleanup
class FirmwareUpdater {
    DiagnosticSession session_;
    SecurityAccess security_;
    FlashMemory flash_;
    EcuReset reset_;
public:
    bool update(const std::vector<int>& image, int crc) {
        if (!session_.enterProgramming()) return false;
        bool ok = security_.unloc
…
session: programming mode
  security: key for seed 4660 accepted
  flash: erased
  flash: wrote 4 blocks
  flash: crc 0xbeef ok
  ecu: hard reset
update succeeded

Step-by-Step Walkthrough

The client calls one method; the facade sequences session, security, flash and reset correctly.

On failure the facade cleans up (exits programming mode); clients don't need to know that rule.

Advanced clients can still use FlashMemory directly — a facade doesn't hide the subsystem, it offers a simpler path.

Common Mistakes

God facade: one SystemFacade with 80 methods for everything. Create task-focused facades.

Business logic creeping into the facade that belongs in domain classes.

Facade as the only path, removing access advanced clients need.

Trade-offs and Alternatives

Facade simplifies; Adapter converts; Mediator (Module 47) coordinates peers that talk to *each other*, whereas subsystem classes behind a facade don't know the facade exists. Application services in layered architectures (Module 71) are facades over the domain.

Real-World Use

SDK "client" classes (S3Client.upload hiding multipart logic), compiler drivers (clang orchestrating preprocessor, compiler, assembler, linker), flashing tools, and std::filesystem::copy over many OS calls.

Practice

Q1. (Easy) Does Facade prevent direct use of subsystem classes?

Answer: No — it offers a simpler path; subsystems remain accessible.

Q2. (Medium) What makes a facade turn into a god class?

Answer: Adding unrelated tasks and business rules to it until it knows everything about everything.

Q3. (Advanced) How would you test FirmwareUpdater without real hardware?

Answer: Make the subsystem classes implement interfaces and inject them (or inject fakes through the constructor); tests assert the call order and that failure paths call exit() instead of reset.

Interview Question

Q1. "Facade vs Mediator?" Answer: Facade is one-directional: clients call the facade, which calls subsystem objects; subsystems don't know about the facade. Mediator centralizes communication *among* colleague objects that know the mediator and notify it; it replaces many-to-many links between peers with many-to-one.

Quick Quiz

Q1. Facade's main benefit?

Answer: Simpler use and lower coupling to subsystem internals.

Q2. Should one facade cover the whole application?

Answer: No — keep facades task-focused.

Q3. Which architectural element is often a facade?

Answer: The application/service layer.

Summary

Facade gives a simple, task-oriented entry point to a subsystem without hiding it.

Keep it thin and focused.

Related: Modules 33, 47, 71.