Category: design patterns Gene De Lisa @ 6:08 pm —
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

Let’s take a look at a class for a Baseball player. In this example
I made it an abstract class but it could just as well have
been an interface for this discussion.
Show/Hide source

And here is an implementation class.
Show/Hide source

And another implementation class.
Show/Hide source

When we test them out like this:

Show/Hide source

We get the following not surprising results:
Show/Hide source

Ignoring the fact that Howard led the league with a
gazillion strikeouts in 2007, what if we want to create
a Phillie object who doesn’t strike out? (Ok, we’re
leaving the real world now…).

Actually one way we could vary individual objects (players) is to
create an anonymous subclass. Here we override the normal
Phillie implementation for swing.
Show/Hide source

That works. Every other Phillie will have the default behavior for
swing and Howard will put it in Ashburn Alley every time. Would
that were true in real life.

What if we wanted to keep the default behavior but do something
extra?

If you simply make a subclass of Player you get the behavior for the
new subclass. The good thing is the subclass is plug compatible with
Player so the client doesn’t have to know what the special behavior is.

But if you pass in any object that is a subclass of Player to
the new subclass’ constructor it can call that
player’s swing method and put special code either before or after
invocation. In essence we are decorating the original player object’s
method invocations. Here we have one abstract method the decorator
must implement (swing) but there could be more of course.

Here’s a jerk decorator who goes after the pitcher after he swings.

Show/Hide source

And this is how you use it.

Show/Hide source

Here when you tell derelictCheater to swing you get the default behavior.
But when you pass the derelictCheater object to the JerkPlayer and tell that
object to swing, you get the default and the new behavior. Creating
another Yankme and telling him to swing shows that only one jerk
plays for the Yankmes (here we leave real life).

Receive check for $10000000000000000
Receive check for $10000000000000000
Charge the mound
Receive check for $10000000000000000

We can copy the Jerk player and make a new class that looks almost
exactly like it. The only change is that the added behavior comes
before the original behavior.

Show/Hide source

And of course you can run it with the same example mutatis mutandis.
(s/Jerk/Nasty/g)

What is really cool though is this. You can decorate a decorator.
Show/Hide source

Great. First Pets and now Baseball players. Give me something
I’d do in real life.

Ever see standard Java code like this?
Show/Hide source

You must be hungry by now. Want a pizza? What do you want on it?
There are probably as many answers to that as there are
people who eat pizza. How would you model pizza as a class?

Show/Hide source

and then a gazillion more subclasses for each topping.

How about combinations of toppings? AnchovyPizza extends what?
Pizza? PepperoniPizza? Well, sometimes you want just anchovies,
sometimes just pepperoni and sometimes both. Remember there is no
multiple implementation inheritance in Java (thank hobgoblins and pointed ears).

Here is a hint. Look at the client and tell me how you would implement your Pizzas.

Show/Hide source

These are some reasons why you would use the GoF Decorator Design Pattern.
Whenever you want to vary individual object’s behaviors with reusable and cascadable behavior
you can implement it like this.

Sorry, no comments yet.

Write Your Comment

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs will be generated automatically.

You should have a name, right? 
Your email address, I promised I won't tell it to anyone. 
If you have a web site or blog, you can type the URL right here. 
This is where you type your comments. 
Remember my information for the next time I visit.