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.
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.
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.
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.