标签:
1、简单工厂模式:又称为静态工厂方法模式,其作用是定义一个创建对象的接口
package com.shma.factory.simplefactory.pizza; import java.util.ArrayList; import java.util.List; /** * 定义一个公共的抽象披萨类 * @author admin * */ public abstract class Pizza { protected String name; protected String dough; protected String sauce; protected List<String> toppings = new ArrayList<String>(); public String getName() { return name; } public void prepare() { System.out.println("Preparing " + name); } public void bake() { System.out.println("Baking " + name); } public void cut() { System.out.println("Cutting " + name); } public void box() { System.out.println("Boxing " + name); } public String toString() { StringBuffer display = new StringBuffer(); display.append("---- " + name + " ----\n"); display.append(dough + "\n"); display.append(sauce + "\n"); for (int i = 0; i < toppings.size(); i++) { display.append((String )toppings.get(i) + "\n"); } return display.toString(); } } package com.shma.factory.simplefactory.pizza; /** * 披萨类型 * @author admin * */ public enum PizzaType { CHESS, CLAM, PEPPERONI, VEGGIE } package com.shma.factory.simplefactory.pizza; /** * 具体的披萨实现类 * @author admin * */ public class CheesePizza extends Pizza { public CheesePizza() { name = "Cheese Pizza"; dough = "Regular Crust"; sauce = "Marinara Pizza Sauce"; toppings.add("Fresh Mozzarella"); toppings.add("Parmesan"); } } package com.shma.factory.simplefactory.pizza; /** * 具体的披萨实现类 * @author admin * */ public class ClamPizza extends Pizza { public ClamPizza() { name = "Clam Pizza"; dough = "Thin crust"; sauce = "White garlic sauce"; toppings.add("Clams"); toppings.add("Grated parmesan cheese"); } } package com.shma.factory.simplefactory.pizza; /** * 具体的披萨实现类 * @author admin * */ public class PepperoniPizza extends Pizza { public PepperoniPizza() { name = "Pepperoni Pizza"; dough = "Crust"; sauce = "Marinara sauce"; toppings.add("Sliced Pepperoni"); toppings.add("Sliced Onion"); toppings.add("Grated parmesan cheese"); } } package com.shma.factory.simplefactory.pizza; /** * 具体的披萨实现类 * @author admin * */ public class VeggiePizza extends Pizza { public VeggiePizza() { name = "Veggie Pizza"; dough = "Crust"; sauce = "Marinara sauce"; toppings.add("Shredded mozzarella"); toppings.add("Grated parmesan"); toppings.add("Diced onion"); toppings.add("Sliced mushrooms"); toppings.add("Sliced red pepper"); toppings.add("Sliced black olives"); } } package com.shma.factory.simplefactory; import com.shma.factory.simplefactory.pizza.CheesePizza; import com.shma.factory.simplefactory.pizza.ClamPizza; import com.shma.factory.simplefactory.pizza.PepperoniPizza; import com.shma.factory.simplefactory.pizza.Pizza; import com.shma.factory.simplefactory.pizza.PizzaType; import com.shma.factory.simplefactory.pizza.VeggiePizza; /** * 简单披萨工厂类 * @author admin * */ public class SimplePizzaFactory { public Pizza createPizza(PizzaType type) { Pizza pizza = null; switch(type) { case CHESS : pizza = new CheesePizza(); break; case CLAM : pizza = new ClamPizza(); break; case PEPPERONI : pizza = new PepperoniPizza(); break; case VEGGIE : pizza = new VeggiePizza(); } return pizza; } } package com.shma.factory.simplefactory; import com.shma.factory.simplefactory.pizza.Pizza; import com.shma.factory.simplefactory.pizza.PizzaType; /** * 披萨商店类 * @author admin * */ public class PizzaStore { private SimplePizzaFactory simplePizzaFactory; public PizzaStore(SimplePizzaFactory simplePizzaFactory) { this.simplePizzaFactory = simplePizzaFactory; } public Pizza orderPizza(PizzaType type) { Pizza pizza = simplePizzaFactory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } package com.shma.factory.simplefactory; import com.shma.factory.simplefactory.pizza.Pizza; import com.shma.factory.simplefactory.pizza.PizzaType; public class PizzaTestMain { public static void main(String[] args) { //创建一个简单披萨工厂 SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory(); //创建一个披萨商店,传递制作披萨的工厂simplePizzaFactory PizzaStore store = new PizzaStore(simplePizzaFactory); //客户下订单制作chess口味的披萨 Pizza pizza = store.orderPizza(PizzaType.CHESS); System.out.println(pizza); //客户下订单制作veggie口味的披萨 pizza = store.orderPizza(PizzaType.VEGGIE); System.out.println(pizza); } }
标签:
原文地址:http://my.oschina.net/shma1664/blog/398416