# Decorator Pattern

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

**Field:** Software Development  
**Category:** Architecture  
**Also known as:** Decorator Design Pattern, Decorator Object

## Think of it like

It is like a coat on top of a jumper. You get warm, and the jumper does not change.

## Definition

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.

## Example code

```python
class CachedReader:
    def __init__(self, inner):
        self.inner, self.cache = inner, {}

    def read(self, key):
        if key not in self.cache:
            self.cache[key] = self.inner.read(key)
        return self.cache[key]
```

CachedReader has the same read() as the reader it wraps, and adds a cache.

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

## Common mistake

Mixing it up with Python's @decorator. That is a language feature for wrapping functions, close in spirit but not the same thing.

## Three principles

- **Same interface.** Callers cannot tell.
- **Wraps and passes on.** Extra work, then the call.
- **Stackable.** One feature per layer.

## Related terms

- [Design Pattern](https://learn.benibix.com/term/development/design-pattern)
- [Adapter Pattern](https://learn.benibix.com/term/development/adapter-pattern)
- [Facade Pattern](https://learn.benibix.com/term/development/facade-pattern)
- [Inheritance](https://learn.benibix.com/term/development/inheritance)
- [Interface (Programming)](https://learn.benibix.com/term/development/interface)
- [Cache](https://learn.benibix.com/term/development/cache)

## Told apart from

- [Decorator Pattern vs Adapter Pattern](https://learn.benibix.com/compare/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.

![Four small panels. Decorator: a reader box wrapped inside a cache layer, wrapped inside a log layer, all with the same read method. Builder: three steps, url, header and timeout, then build, making one finished request. Facade: one place order box in front of three parts, stock, payment and shipping. Repository: app code asks a user repository for a user, and only the repository talks to the database.](https://learn.benibix.com/diagrams/dev6-more-patterns.svg)

---

Source: https://learn.benibix.com/term/development/decorator-pattern
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-18
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
