Head First Design Pattern Question

Still working my way through “Head First Design Patterns”, slowly but surely :-) I have a question for others who have read the book.  In chapter 4, the factory pattern, they use a pizza shop as the example throughout the chapter to show how different pizza shops can have different pizza factories, i.e. Chicago style, New York style, etc.  They then go on to describe how you can use the factory pattern to model the pizzas themselves.

My question is whether or not they could have used the decorator pattern from chapter 3 to model the pizza objects instead of the factory pattern.  The example from the decorator pattern is the coffee shop, with mochas, lattes, blah, blah. It seems natural to use the decorator pattern to model each of the toppings, so you start with a plain cheese pizza, and then decorate it with pepperoni, sausage, peppers, etc.  I realize that they used the factory pattern in order to facilitate the book, but does anyone agree with me that the decorator pattern could have, and maybe should have, been used in a real world scenario?

5 Comments

  1. Anonymous said,

    Wrote on May 24, 2005 @ 3:44 am

    Isn’t Decorator some kind of a last resort? Something you use when you need to change what’s already there, but you can’t do the way you want?

    In this context, Decorator makes sense in the coffe shop, because they are expanding an existing architecture. If the pizza shop is starting from scratch, then you don’t have that limitation.

    I am guessing on the pizza shop, as I haven’t bought the book yet, only read the sample chapter.

  2. breichelt said,

    Wrote on May 24, 2005 @ 5:40 am

    Interesting, I have to go back and look at that chapter again, its been awhile since I read that one, I’ll report back :-)

  3. GooberDLX said,

    Wrote on May 24, 2005 @ 6:41 am

    first: i love the book, great way to introduce patterns to a developer…

    second: i think it was merely an example of the factory pattern. straight up. just as in actual development, there are also a million ways to describe the same thing.

  4. darrell said,

    Wrote on May 24, 2005 @ 7:45 am

    I think Thomas is right about the “last-resort” thing. Decorator ends up having tons of wrappers, and in their example you’d have to do a lot of work to re-order the coffee decorators to say “double-mocha” whereas if you had developed it differently, double-mocha is easy.

  5. Anonymous said,

    Wrote on June 20, 2005 @ 11:43 am

    You could use a decorator pattern to encapsulate the pizza toppings if you wanted to.

    The point of the factory is that the assembling of the class is encapsulated by the factory, so you could swap one factory for another.

Comment RSS