mercredi 20 janvier 2021

object dependency on to concrete classes

I'm trying to understand the abstract factory pattern while it's really hard. I've seen the following example, from Head First Design Patterns book, trying to describe dependencies and why being dependent is not good. However, I don't understand the following saying over that code example.

Because any change to the concrete implementations of pizzas affects the DependentPizzaStore, we say that the DependentPizzaStore “depends on” the pizza implementations.

I don't really understand how it affects that class which just initiates by news and uses bake, cut etc. methods. DependentPizzaStore doesn't know anything about the concrete implementations.

public class DependentPizzaStore {
  public Pizza createPizza(String style, String type) {
    Pizza pizza = null;
    if (style.equals("NY")) {
      if (type.equals("cheese")) {
        pizza = new NYStyleCheesePizza();
      } else if (type.equals("veggie")) {
        pizza = new NYStyleVeggiePizza();
      } else if (type.equals("clam")) {
        pizza = new NYStyleClamPizza();
      } else if (type.equals("pepperoni")) {
        pizza = new NYStylePepperoniPizza();
      }
    } else if (style.equals("Chicago")) {
      if (type.equals("cheese")) {
        pizza = new ChicagoStyleCheesePizza();
      } else if (type.equals("veggie")) {
        pizza = new ChicagoStyleVeggiePizza();
      } else if (type.equals("clam")) {
        pizza = new ChicagoStyleClamPizza();
      } else if (type.equals("pepperoni")) {
        pizza = new ChicagoStylePepperoniPizza();
      }
    } else {
      System.out.println("Error: invalid type of pizza");
      return null;
    }
    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();
    return pizza;
  }
}

Aucun commentaire:

Enregistrer un commentaire