Skip to content
BENIBIX Learn

Decorator Pattern vs Adapter Pattern

People think a decorator and an adapter are the same wrapper. A decorator keeps the same interface and adds a feature, like a log or a cache. Callers cannot tell that anything changed. An adapter changes the interface, so one part can plug into another that wants a new shape. A decorator adds to what a thing does. An adapter changes how you call it.

Development🏗️ Architecture

Decorator Pattern

An object wrapped in another that adds a feature. It still looks the same.

The decorator pattern adds a feature to an object by wrapping it in another object with the same interface. The wrapper does its extra work, then passes the call on to the object inside. You can stack wrappers, each adding one thing, like logging or caching. The code that uses the object cannot tell it is wrapped. It is one of the 23 patterns in the 1994 Gang of Four book.

Example
Jack wraps his data reader in a caching decorator, then in a logging one. The rest of the app still calls read() and never knows.
Why it matters
You add features without changing code that already works, and mix them in any order. It saves writing a new subclass for every mix.
Full entry
Development🏗️ Architecture

Adapter Pattern

A wrapper that lets one piece of code fit another.

The adapter pattern wraps one piece of code so it looks like another. It is used when two parts should work together but expect different shapes. The adapter sits in between and turns each call into the form the other side needs. Neither side has to change. It is common when you plug in an outside library or an old system.

Example
Jack's app expects a send method. The new text library has a postText method. A small adapter class turns one into the other.
Why it matters
It lets you use outside code without letting its shape spread through your whole app.
Full entry

In one line

Decorator Pattern
An object wrapped in another that adds a feature. It still looks the same.
Adapter Pattern
A wrapper that lets one piece of code fit another.