title: Head first design patterns - Factory method pattern VS Abstract factory pattern implementation
body: <p>I am trying to learn the Factory pattern from the <a href="https://rads.stackoverflow.com/amzn/click/com/0596007124" rel="nofollow noreferrer" rel="nofollow noreferrer">Head First Design patterns book</a>. The book says that there are two types of factory pattern - <strong>Factory method and Abstract Factory</strong>. </p>
  
  <p><strong>Why is the code the patterns implemented differently ?</strong> <strong>I am NOT asking the</strong> difference between the patterns, because that has been answered already. Please read further.</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p><strong>Why can't Pizza have the same type of members in both patterns ?</strong> 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 ?</p>
  
  <p><strong>Factory method - Pizza :</strong></p>
  
  <pre><code>public abstract class Pizza {
      String name;
      String dough;
      String sauce;
      ArrayList&lt;String&gt; toppings = new ArrayList&lt;String&gt;();
  
      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");}
  
  }
  </code></pre>
  
  <p><strong>Abstract Factory - Pizza :</strong></p>
  
  <pre><code>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");}
  
  }
  </code></pre>
  
  <p><strong>Diagrams:</strong></p>
  
  <p>Factory method class diagram: </p>
  
  <p><a href="https://i.stack.imgur.com/DCOmh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DCOmh.jpg" alt="Factory method class diagram"></a> </p>
  
  <p>Abstract factory class diagram:</p>
  
  <p><a href="https://i.stack.imgur.com/dYS19.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dYS19.jpg" alt="Abstract factory class diagram"></a></p>