jeudi 2 avril 2020

Head first design patterns - Factory method pattern VS Abstract factory pattern implementation

I am trying to learn the Factory pattern from the Head First Design patterns book. The book says that there are two types of factory pattern - Factory method and Abstract Factory. The book uses the Pizza and PizzaStore example to illustrate the pattern.

We have a PizzaStore that depends on Pizza objects. We have to develop a generic PizzaStore software which allows people to order pizzas. The logical process or steps of Pizza ordering remain the same - create(String pizzaType), prepare() or setupIngredients(), cut(), box() etc. The create() method belongs to PizzaStore and the other methods belong to Pizza.

As we work on the project, we have to add new types of pizzas often. But, we don't want to keep changing our generic PizzaStore code every time a new Pizza is added. After all, the pizza making and ordering process always has the same logical steps.

The book has separate code for each of the two types of factory. For example, the Pizza class below has only String members for name, dough, sauce etc. in factory method. But in abstract factory, it has classes for Dough, Sauce, etc.

Why can't Pizza have the same type of members in both patterns ? In factory method, we could have just one type of Dough instance instead of String dough. In abstract factory, we could have any number of Dough instances. I don't understand the reason for this inconsistency. Is that inconsistency required by the example ?

Factory method - Pizza :

public abstract class Pizza {
    String name;
    String dough;
    String sauce;
    ArrayList<String> toppings = new ArrayList<String>();

    public void prepare() {
        System.out.println("Prepare " + name);
        System.out.println("Tossing dough...");
        System.out.println("Adding sauce...");
        System.out.println("Adding toppings: ");
        for (String topping : toppings) {
            System.out.println("   " + topping);
        }
    }

    public void bake() {System.out.println("Bake for 25 minutes at 350");}

    public void cut() {System.out.println("Cut the pizza into diagonal slices");}

    public void box() {System.out.println("Place pizza in official PizzaStore box");}

    public String getName() {return name;}

    public String toString() {//blah}
}

Abstract Factory - Pizza :

public abstract class Pizza {
    String name;

    Dough dough;
    Sauce sauce;
    Veggies veggies[];
    Cheese cheese;
    Pepperoni pepperoni;
    Clams clam;

    public abstract void prepare();//Prepares the Dough, Sauce, Cheese etc.

    public void bake() {System.out.println("Bake for 25 minutes at 350");}

    public void cut() {System.out.println("Cutting the pizza into diagonal slices");}

    public void box() {System.out.println("Place pizza in official PizzaStore box");}

    public void setName(String name) {this.name = name;}

    public String getName() {return name;}

    public String toString() {//blah}
}

Aucun commentaire:

Enregistrer un commentaire