5

In Head-First Design Patterns, they use a pizza shop example to demonstrate the factory method pattern.

public abstract class PizzaStore {
  public Pizza orderPizza(String type) {
    Pizza pizza;

    pizza = createPizza(type);

    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();

    return pizza;
  }

  abstract Pizza createPizza(String type)
}

public class NYPizzaStore extends PizzaStore {
  Pizza createPizza(String item) {
    if (item.equals("cheese") {
        return new NYStyleCheesePizza();
    } else if (item.equals("veggie")) {
        return new NYStyleVeggiePizza();
    } else if (item.equals("clam")) {
        return new NYStyleClamPizza();
    } else if (item.equals("pepperoni")) {
        return new NYStylePepperioniPizza();
    } else return null;
  }
}

I don't understand how this pattern is not violating open-close. What if we require a beef Pizza, then we must edit the if statement in the NYPizzaStore class.

  • Do they deal with that later in the chapter, or mention it in another chapter? – FrustratedWithFormsDesigner Aug 19 '14 at 18:34
  • Not from what I can find, although I may be incorrect. Surely this example is violating open-close though. – William Aug 19 '14 at 18:44
  • Ah ok. I think I read that book several years ago, and sometimes they'd fix such issues later in the chapter, or reference it in another chapter while suggesting a better solution. Wasn't sure if they did that with this one. – FrustratedWithFormsDesigner Aug 19 '14 at 18:49
  • 3
    You have to crawl before you can walk. If they made these examples with TDD, IoC and Factory Factory Factories from the start, nobody would understand them. – Robert Harvey Aug 19 '14 at 19:22
  • 1
    @Robert Harvey I wasn't trying to pick apart the book, it does a really good job overall if I'm honest. Just wanted to know if am identifying the violation correctly. – William Aug 19 '14 at 19:30
12

Technically, it's a violation, but it's not the worst kind of violation, because it's limited to that one spot. You need some way of adding a new type to the list, and this way is simple and relatively foolproof.

Keep in mind that most programming languages don't have a straightforward way of automatically calling a function when a program loads. You have to do weird things with templates or reflection. That means even if you have some sort of way to dynamically register new pizza types with this factory, there's still some point where main eventually calls the register function for the new pizza type, so you have to add it to some list somewhere. The advantage of putting it in the factory is you aren't initializing any resources for this pizza type until you actually need it.

In practice, it's extremely difficult to write pure OCP code, and usually not worth it. If you're only editing one function, and that function is clearly an initialization list or factory, you're doing pretty well.

0

This example is not a violation of the open-closed principle. You can continue adding new types of pizza by subclassing NYPizzaStore:

class UpmarketNYPizzaStore extends NYPizzaStore {
    Pizza createPizza(String type) {
        if (type.equals("beef")) {
            return new BeefPizza();
        }
        return super.createPizza(type);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.