0

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.

Why is the code the patterns implemented differently ? I am NOT asking the difference between the patterns, because that has been answered already. Please read further.

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");}

}

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");}

}

Diagrams:

Factory method class diagram:

Factory method class diagram

Abstract factory class diagram:

Abstract factory class diagram

karel
  • 5,489
  • 46
  • 45
  • 50
Tom Joe
  • 97
  • 7
  • 2
    You're correct when you say, "_In factory method, we could have just one type of Dough instance instead of String dough._" But since there is only one type in that example, a String is simpler. A String is not required by the example, it's just simpler. Consider the opposite perspective: if the example had used a dough class with only one possible implementation, someone would wonder why it couldn't just be a String. – jaco0646 Apr 03 '20 at 01:32
  • @jaco0646 - I agree. Btw, I wonder why this question was closed. I see many questions about abstract factory vs factory method here already. But, my question is different. I am asking about a particular component of those factories and why that component is implemented differently in the two patterns. – Tom Joe Apr 03 '20 at 02:04
  • 1
    Your question is unique. I suspect it was closed because it is extremely long, and the critical sentence making it unique is buried inside of what otherwise looks very much like a duplicate. A reader has to work very hard to find the actual question here. – jaco0646 Apr 03 '20 at 02:37
  • Some suggestions for future questions: 1.) Pose the question first or last, but not in the middle of a long thread. 2.) Explain how your question is different from other common questions. 3.) Include only enough detail to clarify the question. In this example, the diagrams probably don't add enough value, and the text could be shortened by half. 4.) Try to formulate a unique title. In this example, perhaps, "Head First Factory Patterns Implementation Discrepancy". 5.) Writing clear and articulate questions is hard. It takes practice. – jaco0646 Apr 03 '20 at 02:37
  • @jaco0646 - Thanks. I edited the question. Could you please consider for re-opening ? – Tom Joe Apr 07 '20 at 00:59

0 Answers0