Card 01/ 08
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.
Without the patternsix classes and their order in every client
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
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.
| Pattern | What it does |
|---|---|
| Adapter | Gives an existing object the interface a client expects. |
| Bridge | Separates an abstraction from its implementation so both can vary. |
| Composite | Treats a tree of objects and a single object the same way. |
| Decorator | Adds behaviour by wrapping an object in another with the same interface. |
| Facade (this topic) | Puts one simple interface in front of a complex subsystem. |
| Flyweight | Shares the common part of many small objects to save memory. |
| Proxy | Stands in for an object to control access to it. |
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