Implementing the Decorator Pattern

Implementing the Decorator PatternDaanBlockedUnblockFollowFollowingMay 9Photo by Chris Barbalis on UnsplashThe decorator pattern is a design pattern that makes it able to add behaviour to an instance of a class without affecting the behavior of other instances of the same class.

This can be done dynamically.

The decorator pattern is often used as an alternative to subclassing.

Decorating can provide new behavior for objects at run-time.

Subclassing adds behavior at compile time and affects all instances of the original class.

This difference between decorating and subclassing becomes most important when there are multiple independent ways of extending functionality.

Another reason the decorator pattern is often used is to adhere to the Single Responsibility Principle, since it allows functionality to be encapsulated in different decorator classes.

In this article I will implement the decorator pattern for a bakery, so customers can make their own cake by selecting ingredients.

The price of the cake will be calculated based on the ingredients.

The theory behind the decorator patternThe decorator pattern has one interface, called Component, that gets implemented by two classes: the ConcreteComponent class and the Decorator class.

The Decorator class has an instance of Component, which could either be an instance of ConcreteComponent or Decorator.

This makes it possible to stack multiple decorators on top of each other, which adds new functionality to the overridden method(s).

The last type of class that the decorator pattern has are the ConcreteDecorator classes.

This are the classes that decorate the Component by adding behaviour to it.

Decorator Pattern UMLLet’s start implementingFirst of all, let’s start by updating the UML-diagram for this example.

UML for this exampleIn this example the Cake interface has two methods: ingredients and price , which will return the ingredients and the price of the cake, respectively.

The CakeDecorator is an abstract class that forces every concrete decorator that extends the CakeDecorator to implement their own version of the ingredients and price methods.

Note that the CakeDecorator gets an instance of Cake, which makes chaining possible.

The SimpleCake class is an example of a Component class that can be decorated.

It is very easy to extend this functionality.

If the bakery wants to add an extra option for chocolate cakes, all that has to be done is create a new component, called ChocolateCake.

Existing code remains untouched.

The WithWhippedCream is a concrete decorator class that extends the CakeDecorator.

This class uses the $cake property of the parent to add behaviour to (decorate) the parent.

The WithSprinkles class looks pretty much the same as the WithWhippedCream class.

Difference between these classes is that they each decorate their parent in a different way.

Both have a different price and different ingredients.

See how easy it is to extend functionality?.Let’s say that the bakery wants to give customers more options to decorate their cakes.

The bakery has the brilliant idea to start selling strawberries.

All we have to do is create a new concrete decorator called WithStrawberries.

Again, existing code remains untouched.

It is time to make our own cake!Now that all the hard work is done, it is time to start making ourselves some cake.

That’s it!.We have now succesfully implemented the decorator pattern.

If this article helped you in any way make sure to check out my other posts aswell.

Please feel free to leave a comment if you have any feedback, questions or want me to write about another programming related topic.

.

. More details

Leave a Reply