DELAPARRA
Index
Color
NL
Category: Design patterns

Decorator

Explained by Updated

What is Decorator?

Decorator: Attach additional responsibilities to an object dynamically — a flexible alternative to subclassing for extending behaviour. The Decorator pattern adds behaviour to an object by wrapping it in another object with the same interface. The wrapper does its extra work and passes the call through to the original. A classic example is caching. A CachedProductRepository wraps the normal ProductRepository. It checks the cache first and only asks the real repository when the value is missing. The rest of the application does not notice the difference.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Decorator

What should you keep in mind with Decorator?

Decorators can be stacked: logging around caching around the real implementation. That avoids deep inheritance trees where every combination needs its own subclass. In Laravel, you can bind a decorated class in the service container, so the change happens in one place. Keep each decorator focused on one concern, so the stack stays easy to follow.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with D

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.