design patterns · medium
Facade Pattern
Facade is a structural GoF pattern that provides a single, unified, high-level interface over a complex subsystem of many classes, making it easier to use — like a waiter coordinating the kitchen, bar, and till so the diner just orders a dish. The facade (e.g. MediaPlayer.play) encapsulates the precise choreography of the subsystem classes (Codec, Buffer, Device) behind one method, so callers depend only on the facade and the subsystem can be refactored behind it. It reduces coupling and forms a clean layering boundary, but it does NOT seal the subsystem off — advanced users can still access the classes directly; keep the facade thin (delegation and ordering, not business logic, or it becomes a god-object). Use it when a subsystem is complex or order-sensitive and callers need only the common path. It differs from Adapter (which converts one interface to match a client) and Mediator (which bidirectionally coordinates peers) — Facade is a one-way simplifier that the subsystem is unaware of.
Facade (structural): one simple high-level interface over a complex multi-class subsystem (a waiter over the kitchen). It reduces coupling and creates a layer boundary, but doesn't block direct subsystem access; keep it thin (delegation + ordering). Adapter converts ONE interface to fit a client; Facade SIMPLIFIES many interfaces into one new one.
The code
// Facade — one simple entry point over a complex subsystem.// Subsystem: many classes, fiddly order of operations.class Codec {public: void open(); void decode(Frame&);};class Buffer {public: void alloc(int); void fill(Frame&);};class Device {public: void init(); void render(Buffer&);};
// Facade: hides the wiring behind ONE method.class MediaPlayer { Codec codec_; Buffer buf_; Device dev_;
public: void play(const std::string& file) { // the simple API codec_.open(); dev_.init(); buf_.alloc(4096); Frame f; codec_.decode(f); buf_.fill(f); dev_.render(buf_); }};
MediaPlayer().play("clip.mp4"); // client does ONE callWhat this lesson walks through
- 01Intent — one simple call over a complex subsystem
- 02It simplifies — it does not hide
- 03Facade orchestrates the messy steps
- 04Gotcha — don't grow a god-object
A Facade gives a small, friendly interface to a big, complicated subsystem. The client calls computer.start() and the facade runs the dozen low-level steps behind it — the client never learns the subsystem's internals.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of Facade Pattern and 100+ animated C++ interview lessons.