设计模式:工厂

妖精的绣舞 提交于 2019-12-26 17:10:44

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

模拟不同披萨店制作不同口味的披萨的情景。

 

首先定义main方法,它需要各种各样的披萨。

public class PizzaTestDrive {
 
   public static void main(String[] args) {
      PizzaStore nyStore = new NYPizzaStore();
      PizzaStore chicagoStore = new ChicagoPizzaStore();
 
      Pizza pizza = nyStore.orderPizza("cheese");
      System.out.println("Ethan ordered a " + pizza + "\n");
 
      pizza = chicagoStore.orderPizza("cheese");
      System.out.println("Joel ordered a " + pizza + "\n");

      pizza = nyStore.orderPizza("clam");
      System.out.println("Ethan ordered a " + pizza + "\n");
 
      pizza = chicagoStore.orderPizza("clam");
      System.out.println("Joel ordered a " + pizza + "\n");

      pizza = nyStore.orderPizza("pepperoni");
      System.out.println("Ethan ordered a " + pizza + "\n");
 
      pizza = chicagoStore.orderPizza("pepperoni");
      System.out.println("Joel ordered a " + pizza + "\n");

      pizza = nyStore.orderPizza("veggie");
      System.out.println("Ethan ordered a " + pizza + "\n");
 
      pizza = chicagoStore.orderPizza("veggie");
      System.out.println("Joel ordered a " + pizza + "\n");
   }
}

由上可知,它找到两个披萨店,然后定分别在两家店定了四分不同披萨。接下来我们看看这两家店是怎么做的。

public abstract class PizzaStore {
 
   protected abstract Pizza createPizza(String item);
 
   public Pizza orderPizza(String type) {
      Pizza pizza = createPizza(type);
      System.out.println("--- Making a " + pizza.getName() + " ---");
      pizza.prepare();
      pizza.bake();
      pizza.cut();
      pizza.box();
      return pizza;
   }
}

第一家店

public class NYPizzaStore extends PizzaStore {
 
   protected Pizza createPizza(String item) {
      Pizza pizza = null;
      PizzaIngredientFactory ingredientFactory = 
         new NYPizzaIngredientFactory();
 
      if (item.equals("cheese")) {
  
         pizza = new CheesePizza(ingredientFactory);
         pizza.setName("New York Style Cheese Pizza");
  
      } else if (item.equals("veggie")) {
 
         pizza = new VeggiePizza(ingredientFactory);
         pizza.setName("New York Style Veggie Pizza");
 
      } else if (item.equals("clam")) {
 
         pizza = new ClamPizza(ingredientFactory);
         pizza.setName("New York Style Clam Pizza");
 
      } else if (item.equals("pepperoni")) {

         pizza = new PepperoniPizza(ingredientFactory);
         pizza.setName("New York Style Pepperoni Pizza");
 
      } 
      return pizza;
   }
}

第二家店

public class ChicagoPizzaStore extends PizzaStore {

   protected Pizza createPizza(String item) {
      Pizza pizza = null;
      PizzaIngredientFactory ingredientFactory =
      new ChicagoPizzaIngredientFactory();

      if (item.equals("cheese")) {

         pizza = new CheesePizza(ingredientFactory);
         pizza.setName("Chicago Style Cheese Pizza");

      } else if (item.equals("veggie")) {

         pizza = new VeggiePizza(ingredientFactory);
         pizza.setName("Chicago Style Veggie Pizza");

      } else if (item.equals("clam")) {

         pizza = new ClamPizza(ingredientFactory);
         pizza.setName("Chicago Style Clam Pizza");

      } else if (item.equals("pepperoni")) {

         pizza = new PepperoniPizza(ingredientFactory);
         pizza.setName("Chicago Style Pepperoni Pizza");

      }
      return pizza;
   }
}

注意这两家店的原料原料供应商来自不同的工厂。

先看作为供应商需要提供什么东西。

public interface PizzaIngredientFactory {
 
   public Dough createDough();
   public Sauce createSauce();
   public Cheese createCheese();
   public Veggies[] createVeggies();
   public Pepperoni createPepperoni();
   public Clams createClam();
 
}

两家供应商的分别实现。

public class ChicagoPizzaIngredientFactory 
   implements PizzaIngredientFactory 
{

   public Dough createDough() {
      return new ThickCrustDough();
   }

   public Sauce createSauce() {
      return new PlumTomatoSauce();
   }

   public Cheese createCheese() {
      return new MozzarellaCheese();
   }

   public Veggies[] createVeggies() {
      Veggies veggies[] = { new BlackOlives(), 
                            new Spinach(), 
                            new Eggplant() };
      return veggies;
   }

   public Pepperoni createPepperoni() {
      return new SlicedPepperoni();
   }

   public Clams createClam() {
      return new FrozenClams();
   }
}
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {
 
   public Dough createDough() {
      return new ThinCrustDough();
   }
 
   public Sauce createSauce() {
      return new MarinaraSauce();
   }
 
   public Cheese createCheese() {
      return new ReggianoCheese();
   }
 
   public Veggies[] createVeggies() {
      Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() };
      return veggies;
   }
 
   public Pepperoni createPepperoni() {
      return new SlicedPepperoni();
   }

   public Clams createClam() {
      return new FreshClams();
   }
}

以上实现了接口方法,说明了他们能提供的不同口味的东西。

原料都备齐了。现在开始做披萨了。

public abstract class Pizza {
   String name;

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

   abstract void prepare();

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

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

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

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

   String getName() {
      return name;
   }

   public String toString() {
      StringBuffer result = new StringBuffer();
      result.append("---- " + name + " ----\n");
      if (dough != null) {
         result.append(dough);
         result.append("\n");
      }
      if (sauce != null) {
         result.append(sauce);
         result.append("\n");
      }
      if (cheese != null) {
         result.append(cheese);
         result.append("\n");
      }
      if (veggies != null) {
         for (int i = 0; i < veggies.length; i++) {
            result.append(veggies[i]);
            if (i < veggies.length-1) {
               result.append(", ");
            }
         }
         result.append("\n");
      }
      if (clam != null) {
         result.append(clam);
         result.append("\n");
      }
      if (pepperoni != null) {
         result.append(pepperoni);
         result.append("\n");
      }
      return result.toString();
   }
}

除了准备材料,其他步骤都是相同的。所以,要做不同类的披萨,其区别只在于用的材料不相同。

它的实现类只要复写其准备材料的方法就行。

public class CheesePizza extends Pizza {
   PizzaIngredientFactory ingredientFactory;
 
   public CheesePizza(PizzaIngredientFactory ingredientFactory) {
      this.ingredientFactory = ingredientFactory;
   }
 
   void prepare() {
      System.out.println("Preparing " + name);
      dough = ingredientFactory.createDough();
      sauce = ingredientFactory.createSauce();
      cheese = ingredientFactory.createCheese();
   }
}

由上面例子可知,工厂模式模拟不同供应商。提供多种多样的选择。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!