loading...

Design Patterns: Decorator Pattern

henriguy profile image Henri Idrovo Originally published at henricodesjava.blog on ・4 min read

originally posted on my blog at henricodesjava.blog

Hi everyone! Chapter three in Head First Design Patterns: A Brain-Friendly Guide introduces us to the Decorator pattern. A pattern that is heavily used in the JDK’s java.io package. And although not perfect, it can be combined with Factory and Builder pattern (not yet covered) to make them more useful and clean. Let’s talk about the Decorator pattern.

A new design principle is introduced in this chapter. Classes should be open for extension, but closed for modification. And it helps us understand that inheritance is only one form of extension, but not necessarily the best way to achieve flexibility in our designs. The pattern favors composition and delegation to add new behaviors during runtime, rather than during compile-time. In our designs we should allow behavior to be extended without the need to modify existing code. Lets give this pattern a formal definition.

Decorator Pattern: This pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

This definition doesn’t really tell us how this pattern is implemented, so lets take a look at a class diagram definition. I wont reproduce the class diagram in the text, I’ll give another slightly different diagram. In the diagram below ‘Component’ is an interface instead of an abstract class, but the general idea is still the same.

Screen Shot 2017-10-06 at 11.28.57 AM.png

Now let me give a concrete example thats a little different from the text. Lets say we are starting our own taco stand restaurant, and we need to write some code for our taco stand’s ordering system. And lets say our starting point is a ‘Taco’ interface.

Screen Shot 2017-10-05 at 9.59.22 PM

There are four types of tacos we want to serve: beef, chicken, pork, and veggie. Then there are four condiments we can add to a taco: cilantro, onion, avocado, and potato (we don’t put lettuce or tomato on our tacos). Now lets show a class diagram four our taco stand’s ordering system that implements the decorator patten design.

Screen Shot 2017-10-06 at 11.22.11 AM.png

And now lets take a look at some actual code implementing this design. First the Taco interface(component).

Screen Shot 2017-10-06 at 11.03.02 AM.png

Next some Taco implementations (concrete components).

Screen Shot 2017-10-06 at 11.05.43 AM.png

Screen Shot 2017-10-06 at 11.07.28 AM.png

Next lets look at the Condiment Decorator (Decorator).

Screen Shot 2017-10-06 at 11.09.13 AM.png

Now some condiment implementations (concrete decorators).

Screen Shot 2017-10-06 at 11.11.31 AM.png

Screen Shot 2017-10-06 at 11.12.05 AM.png

The Decorator Pattern involves a set of decorator classes(cilantro, onion, avocado, potato) that are used to wrap concrete components (beef, chicken, pork, veggie). So lets take a look at some code that glues this all together. Note that we will learn about much better ways of creating decorated objects when we cover the Factory and Builder design patterns.

Screen Shot 2017-10-06 at 11.25.27 AM.png

Good news. I checked in all this code and verified it works as expected. If you are curious you can check it out on GitHub here.

That is it! That is the Decorator pattern in action, thanks for reading !👋🏽

Discussion

pic
Editor guide
Collapse
julipatrush profile image
Juli Patrush

Hey, Henri! Nice article, good explained ;)
I've got a question on Java: you defined in the Taco interface a method cost as abstract. Why? why not leave the method without "abstract"?

PS: Gonna check your blog on having more similar articles ;)

Collapse
henriguy profile image
Henri Idrovo Author

Hi Juli. Good question. The terms 'public' and 'abstract' in the Taco interface are redundant. They were left over from the textbook example that used an abstract class. Thanks for pointing that out! Hope you find my posts useful :-)

Collapse
kabircse profile image
Kabir Hossain

Hi Henri, Thanks for your nice article.
I know the basic of design pattern. But when I am coding to develop a software in my real life, I can not apply it. Could you give me a guide line ?