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

DimensionAdapterDecoratorProxy
Primary intentConvert interface A → interface BAdd behaviour, same interfaceControl access, same interface
Interface changeYes — wraps to a different interfaceNo — preserves same interfaceNo — preserves same interface
Knows about wrapped objectYesYesYes (usually)
Wraps pre-existing objectAlways (retrospective)YesMay create the real object lazily
Relationship to wrappedStructural — makes incompatible workAdditive — enhancesSurrogate — controls access
Typical use caseThird-party API integrationLogging, caching, validation wrappersLazy init, access control, remote stub
GoF pagep.139p.175p.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

Notes that link here: Adapter-Pattern, Decorator-Pattern, Proxy-Pattern, GoF-Patterns-MOC