Learning path · 7 · intermediate
GoF structural patterns
Seven patterns about composing classes and objects into larger structures: Adapter, Bridge, Composite, Decorator, Facade, Flyweight and Proxy. Each with a real-life problem, a diagram and code in five languages.
- Adapter – how to get along with code that speaks another language
Adapter translates the interface of a foreign class into the interface your code expects. An example of integrating a third-party geocoding SDK in C#, TypeScript, Java, Go and Python.
Start here: make an incompatible interface fit.
- Bridge – two axes of change without a class explosion
Bridge separates the abstraction (what we do) from the implementation (how we do it), so both hierarchies can evolve independently. An example of notifications and delivery channels in C#, TypeScript, Java, Go and Python.
Two dimensions of change that grow independently.
- Composite – trees in which a leaf and a branch look the same
Composite lets you treat a single object and a whole group of objects in exactly the same way. A course curriculum example with modules and lessons in C#, TypeScript, Java, Go and Python.
Trees where a leaf and a branch look the same.
- Decorator – adding behaviour layer by layer
Decorator wraps an object in another object with the same interface and adds behaviour before or after the call. An example of caching, retries and logging for an exchange rate provider in C#, TypeScript, Java, Go and Python.
Add behaviour by wrapping, not by subclassing.
- Facade – one simple entry point to a complex subsystem
Facade exposes one convenient operation that coordinates many subsystem classes behind the scenes. An example of course enrollment with payment, access and a welcome email in C#, TypeScript, Java, Go and Python.
One simple entry point to a complex subsystem.
- Flyweight – how to fit 60,000 stadium seats in memory
Flyweight separates the state shared by many objects from the state unique to each one, and shares the former. A stadium seat map example from a ticket booking system in C#, TypeScript, Java, Go and Python.
Share state to fit millions of objects in memory.
- Proxy – a stand-in that controls access to the real object
Proxy has the same interface as the real object and stands in front of it, deciding when – and whether – to pass a call on. An invoice archive example with access control and a lazy connection in C#, TypeScript, Java, Go and Python.
A stand-in that controls access to the real object.