Decorator vs Proxy vs Adapter
Decorator vs Proxy vs Adapter
Three structural patterns that all wrap an object — distinguished by intent: interface conversion (Adapter), behaviour addition (Decorator), or access control (Proxy).
Quick Decision Rule
- "I need it to look like X but it currently is Y (incompatible interface)" — Adapter
- "I need to add behaviour without changing the interface" — Decorator
- "I need to control when or how the real object is accessed" — Proxy
Comparison Table
| Dimension | Adapter | Decorator | Proxy |
|---|---|---|---|
| Primary intent | Convert interface A → interface B | Add behaviour, same interface | Control access, same interface |
| Interface change | Yes — wraps to a different interface | No — preserves same interface | No — preserves same interface |
| Knows about wrapped object | Yes | Yes | Yes (usually) |
| Wraps pre-existing object | Always (retrospective) | Yes | May create the real object lazily |
| Relationship to wrapped | Structural — makes incompatible work | Additive — enhances | Surrogate — controls access |
| Typical use case | Third-party API integration | Logging, caching, validation wrappers | Lazy init, access control, remote stub |
| GoF page | p.139 | p.175 | p.207 |
Common Confusion Cases
Decorator vs Proxy: Both preserve the same interface. The difference is intent: Decorator adds new behaviour (a TimestampDecorator adds timestamps); Proxy controls access (a ProxyImage defers loading until needed). If you are adding a responsibility, use Decorator. If you are controlling access or adding a layer between client and the real object, use Proxy.
Adapter vs Decorator: Both wrap an object. Adapter's client calls through a different interface than the wrapped object exposes. Decorator's client calls through the same interface. If the interface changes, it is an Adapter.
Adapter vs Proxy: Adapter converts interfaces; Proxy preserves them. A Remote Proxy looks like the real object locally — same interface — but forwards calls to a remote instance. An Adapter makes an Adaptee look like a Target the client already knows.
Pattern Notes
Backlinks
Notes that link here: Adapter-Pattern, Decorator-Pattern, Proxy-Pattern, GoF-Patterns-MOC