DESIGN PATTERNS SERIES

How to Dynamically Add Behaviors With Decorator Pattern

Everything you need to know about Decorator

Daniel Rusnok
The Startup
Published in
7 min readMay 27, 2020

--

Created by www.freepik.com

What is the Decorator Pattern?

The Decorator Pattern is a structural design pattern that allows you to dynamically add behavior to a class without making changes to that class. In other words, it uses composition instead of inheritance to add the functionality of an object.

Elements of the Decorator Pattern

IComponent

IComponent is an interface. It is an abstraction of what we want to decorate. You can also use an abstract class.

Component

The component is a class that you want to add behavior. The component needs to implement an interface or extend an abstract class.

Decorator

The decorator class will implement the same interface or abstract class as the Component class.

The decorator’s constructor always takes an object implementing the same interface as a…

--

--

Daniel Rusnok
The Startup

Senior Software Developer. Loving to gain & share knowledge. Focused on Microsoft technologies like Azure, .NET Core & C#. Software Architecture enthusiasist.