Design Patterns: Decorator Pattern
Tags: patterns
Published: 2017-10-06 17:56:39

---
title: "Design Patterns: Decorator Pattern"
published: true
tags: patterns
canonical_url: https://henricodesjava.blog/2017/10/06/design-patterns-decorator-pattern/
---

_originally posted on my blog at [henricodesjava.blog](https://henricodesjava.blog/)_

Hi everyone! Chapter three in [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&qid=1505068308&sr=8-1&keywords=head+first+design+patterns) 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](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-28-57-am.png?w=574&h=613)

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](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-05-at-9-59-22-pm.png?w=194&h=175)

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](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-22-11-am.png?w=900)

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](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-03-02-am1.png?w=353&h=162)

Next some Taco implementations (concrete components).

![Screen Shot 2017-10-06 at 11.05.43 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-05-43-am.png?w=452&h=187)

![Screen Shot 2017-10-06 at 11.07.28 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-07-28-am.png?w=456&h=165)

Next lets look at the Condiment Decorator (Decorator).

![Screen Shot 2017-10-06 at 11.09.13 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-09-13-am.png?w=431&h=137)

Now some condiment implementations (concrete decorators).

![Screen Shot 2017-10-06 at 11.11.31 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-11-31-am.png?w=578&h=201)

![Screen Shot 2017-10-06 at 11.12.05 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-12-05-am.png?w=580&h=203)

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](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-06-at-11-25-27-am.png?w=1076)

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](https://github.com/henriguy1210/DecoratorPattern).

That is it! That is the Decorator pattern in action, thanks for reading  !ðŸ‘‹ðŸ½

  
[![](http://feeds.wordpress.com/1.0/comments/henricodesjava.wordpress.com/331/)](http://feeds.wordpress.com/1.0/gocomments/henricodesjava.wordpress.com/331/) ![](https://pixel.wp.com/b.gif?host=henricodesjava.blog&blog=133476896&post=331&subd=henricodesjava&ref=&feed=1)