
Albert Bennett
Design Patterns: Decorator
Updated: Jul 12, 2021
As always here is the link to the GitHub repo :)
If this post has helped you feel free to like, comment and share.
So the Decorator pattern is great for attaching additional functionality to an object without the hassle of sub-classing. Here is a diagram of how this pattern is structured:

As you can see the process of adding additional functionality involves adding Components to a Decorator class, which it itself is a component.
For our use case we want to create a program that put a chassis on a car.
Component (Wheels) : Components is where the core functionality is held. It's also the base class for Decorators.

Decorator (CarBody, Chassis): This a container for our Component. It's "Build" function gets called when we want to the referenced Component to do something. As Decorators themselves are components they can be attached to other Decorators.

This is an example implementation our Decorator pattern. Notice how Decorators can be added to other Decorators. Also the Decorator would be the object that gets passed around and not the components.

I hope that this has helped you to understand the Decorator pattern.
See you later ᕕ( ᐛ )ᕗ