Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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);
    }
}
Licenciado em: CC-BY-SA com atribuição
scroll top