Handbooks / OOP & Design Patterns / Chapter 7
Structural Patterns
7 pages · ~5 min✓ Reviewed
Builds on Creational Patterns. Next up: Behavioral Patterns.
Structural Patterns Mental Model
1. One Skeleton
- Compose classes and objects into bigger structures; behaviour contracts stay unchanged
- Favour composition over inheritance
- Wrapper holds a reference to the wrapped object and forwards calls
- Patterns differ in intent, not in shape
- Client
- Wrapperholds a reference
- Wrappeddoes the work
2. Interface Decides the Pattern
| Wrapper's interface | Patterns |
|---|---|
| Changes | Adapter, Facade |
| Identical | Decorator, Proxy, Composite |
Adapter and Facade: Changing the Interface
1. Adapter
- Client expects Target; existing Adaptee has an incompatible interface
- Adapter implements Target, translates each call to the Adaptee
- Object adapter: holds adaptee by composition, preferred, works with subclasses
- Class adapter: inherits adaptee via multiple inheritance, rare
typescript
class XmlToJsonAdapter implements JsonSource { constructor(private xml: XmlSource) {} read() { return toJson(this.xml.read()); } }
object adapter: translates read()
2. Facade
- One simplified entry point over a complex subsystem
Decorator and Proxy: Same Interface, Different Intent
1. Decorator: add behaviour
- Wraps an object with the same interface, adds work before or after delegating
- Decorators stack; the client picks the order
- Java:
new BufferedReader(new FileReader(f)) - Python:
@decoratoradds logging or caching around a call
new Gzip(new Encrypt(new FileStream()))
- 1Gzipcompress
- 2Encryptthen encrypt
- 3FileStreamwrites
2. Proxy: control access
- Stand-in with the same interface that guards the real object
- Virtual: lazy-load the costly object
Composite and Bridge: Hierarchies and Abstractions
1. Composite: Tree as One Object
- Component interface shared by Leaf and Composite
- Composite holds child Components, forwards calls
- Client treats one object and a tree alike
python
class File: def size(self): return self.bytes class Folder: def size(self): return sum(c.size() for c in self.children)
Folder recurses; File is the base case
add(),remove()on Composite: safe- On Component: transparent, leaves fail
- Trade-off: transparency vs safety
Choosing Between the Six Patterns
1. Pick by Intent
| Need | Pattern |
|---|---|
| Incompatible interface | Adapter |
| Simplify a subsystem | Facade |
| Add behaviour dynamically | Decorator |
| Control access or lazy-load | Proxy |
| Tree of uniform parts | Composite |
| Two independent dimensions of variation | Bridge |
Structural Patterns Cheat Sheet
1. Wrappers at a Glance
| Pattern | Intent | Interface | Example |
|---|---|---|---|
| Adapter | convert A to B; wraps one object | changes | legacy SDK wrapper |
| Facade | simplify a subsystem | new, simpler | VideoConverter.convert() |
| Decorator | add behaviour; stackable | same | Gzip(Encrypt(Stream)) |
Check yourself
You call Gzip(Encrypt(FileStream())).write(data). In what order is the data transformed?
- Compressed first, then encrypted: the outer decorator runs first, then delegates inward.
- Swapping the order would compress ciphertext, which barely shrinks.
ReportProxy implements Report and wraps RealReport. Its load() catches every exception and returns an empty report. What is wrong?
- It swallows errors, so callers can no longer tell failure from an empty result.
- A proxy must keep the real object's contract, not hide its failures.