The decorator design pattern

There are cases where we might want to add some extra functionality to a class in an application. This could be done via inheritance; however, we might not want to do this or affect all the other classes in our application. This is where the decorator design pattern is useful.


The purpose of the decorator design pattern is to add functionality to objects without extending them and without affecting the behavior of other objects from the same class.

The decorator design pattern works by wrapping the decorated object and it can be applied during runtime. Decorators are extremely useful in the cases where there could be multiple extensions of a class and they could be combined in various ways. Instead of writing all ...

Get Scala Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.