ConceptDifficulty: Intermediate3 min

Facade Pattern: One Simple Interface Over a Complex Subsystem

Converting a video takes six classes in the right order; a facade offers convert(file, format).

The Facade pattern provides one simple interface to a set of classes, a subsystem, that together do a job. The facade is a class with a few methods for the common cases, each calling the subsystem's classes in the right order, so a client calls one method instead of learning every class. This card covers the problem a facade solves, the intent, and when it is the right tool.

The problem appears when a job needs many classes in sequence. Converting a video means opening the file, choosing a codec, decoding the frames, resampling the audio, encoding, and writing the result. A client that does this itself depends on six classes and the order they must be called in, and so does every other client that converts a video.

A client that drives the subsystem, and one that calls a facade

Without the patternsix classes and their order in every client

py
source = VideoFile("talk.ogg")
codec = CodecFactory.extract(source)
frames = Decoder(codec).decode(source)
audio = AudioMixer().fix(frames)
encoded = Encoder(Mp4Codec()).encode(frames, audio)
FileWriter("talk.mp4").write(encoded)

Every client that converts a video repeats these six lines and their order.

With the patternone call for the common case

py
converter = VideoConverter()
converter.convert("talk.ogg", "mp4")

The sequence lives in the facade, and the client knows one class.

The Gang of Four state the intent as: provide a unified interface to a set of interfaces in a subsystem, defining a higher-level interface that makes the subsystem easier to use. "Higher-level" means that the facade speaks in the client's terms, convert this file, rather than in the subsystem's, decode these frames.

Think of a hotel concierge who books the restaurant, the taxi and the theatre in one conversation. The analogy holds further than most: a guest can still ring the restaurant directly, and nothing about the concierge prevents it. A facade adds a convenient path and closes none of the others.

Facade is one of seven structural patterns, the family that composes classes and objects into larger structures.

The structural family, with this pattern marked
PatternWhat it does
AdapterGives an existing object the interface a client expects.
BridgeSeparates an abstraction from its implementation so both can vary.
CompositeTreats a tree of objects and a single object the same way.
DecoratorAdds behaviour by wrapping an object in another with the same interface.
Facade (this topic)Puts one simple interface in front of a complex subsystem.
FlyweightShares the common part of many small objects to save memory.
ProxyStands in for an object to control access to it.
Facade

Use it when

  • Most clients need the same few jobs from a subsystem of many classes.
  • Clients should depend on one stable class while the subsystem behind it changes.
  • A subsystem is being layered, and each layer should be entered through one point.

Avoid it when

  • The subsystem is already one or two classes with a simple interface.
  • Clients mostly need the detailed control the subsystem offers, so the facade would be bypassed.
  • The job is to change one object's interface to another, which is Adapter.

← → move through sections and cards