简单工厂模式
需求1:
看一个披萨的项目:要便于披萨种类的扩展,要便于维护
1)披萨的种类很多,比如GreekPizz 、 CheesePizz 等
2)披萨的制作有 prepare bake, cut, box
3)完成披萨店订购功能。
传统实现
Pizza 类
//将Pizza 类做成抽象 public abstract class Pizza { protected String name; //名字 //准备原材料, 不同的披萨不一样,因此,我们做成抽象方法 public abstract void prepare(); public void bake() { System.out.println(name + " baking;"); } public void cut() { System.out.println(name + " cutting;"); } //打包 public void box() { System.out.println(name + " boxing;"); } public void setName(String name) { this.name = name; } }
CheesePizza类
public class CheesePizza extends Pizza { @Override public void prepare() { // TODO Auto-generated method stub System.out.println(" 给制作奶酪披萨 准备原材料 "); } } GreekPizza public class GreekPizza extends Pizza { @Override public void prepare() { // TODO Auto-generated method stub System.out.println(" 给希腊披萨 准备原材料 "); } }
PepperPizza
public class PepperPizza extends Pizza { @Override public void prepare() { // TODO Auto-generated method stub System.out.println(" 给胡椒披萨准备原材料 "); } }
OrderPizza 订购pizza
public class OrderPizza { // 构造器 public OrderPizza() { Pizza pizza = null; String orderType; // 订购披萨的类型 do { orderType = getType(); if (orderType.equals("greek")) { pizza = new GreekPizza(); pizza.setName(" 希腊披萨 "); } else if (orderType.equals("cheese")) { pizza = new CheesePizza(); pizza.setName(" 奶酪披萨 "); } else if (orderType.equals("pepper")) { pizza = new PepperPizza(); pizza.setName("胡椒披萨"); } else { break; } //输出pizza 制作过程 pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } // 写一个方法,可以获取客户希望订购的披萨种类 private String getType() { try { BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input pizza 种类:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } }
测试类:PizzaStore
//相当于一个客户端,发出订购 public class PizzaStore { public static void main(String[] args) { new OrderPizza(); } }
运行结果:
优缺点:
简单工厂方式实现
基本介绍
类:
其中: 这下面的4个类代码不变。
SimpleFactory
//简单工厂类 public class SimpleFactory { //更加orderType 返回对应的Pizza 对象 public Pizza createPizza(String orderType) { Pizza pizza = null; System.out.println("使用简单工厂模式"); if (orderType.equals("greek")) { pizza = new GreekPizza(); pizza.setName(" 希腊披萨 "); } else if (orderType.equals("cheese")) { pizza = new CheesePizza(); pizza.setName(" 奶酪披萨 "); } else if (orderType.equals("pepper")) { pizza = new PepperPizza(); pizza.setName("胡椒披萨"); } return pizza; } //简单工厂模式 也叫 静态工厂模式 public static Pizza createPizza2(String orderType) { Pizza pizza = null; System.out.println("使用简单工厂模式2"); if (orderType.equals("greek")) { pizza = new GreekPizza(); pizza.setName(" 希腊披萨 "); } else if (orderType.equals("cheese")) { pizza = new CheesePizza(); pizza.setName(" 奶酪披萨 "); } else if (orderType.equals("pepper")) { pizza = new PepperPizza(); pizza.setName("胡椒披萨"); } return pizza; } }
OrderPizza
public class OrderPizza { // 构造器 // public OrderPizza() { // Pizza pizza = null; // String orderType; // 订购披萨的类型 // do { // orderType = getType(); // if (orderType.equals("greek")) { // pizza = new GreekPizza(); // pizza.setName(" 希腊披萨 "); // } else if (orderType.equals("cheese")) { // pizza = new CheesePizza(); // pizza.setName(" 奶酪披萨 "); // } else if (orderType.equals("pepper")) { // pizza = new PepperPizza(); // pizza.setName("胡椒披萨"); // } else { // break; // } // //输出pizza 制作过程 // pizza.prepare(); // pizza.bake(); // pizza.cut(); // pizza.box(); // // } while (true); // } //定义一个简单工厂对象 SimpleFactory simpleFactory; Pizza pizza = null; //构造器 public OrderPizza(SimpleFactory simpleFactory) { setFactory(simpleFactory); } public void setFactory(SimpleFactory simpleFactory) { String orderType = ""; //用户输入的 this.simpleFactory = simpleFactory; //设置简单工厂对象 do { orderType = getType(); pizza = this.simpleFactory.createPizza(orderType); //输出pizza if(pizza != null) { //订购成功 pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } else { System.out.println(" 订购披萨失败 "); break; } }while(true); } // 写一个方法,可以获取客户希望订购的披萨种类 private String getType() { try { BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input pizza 种类:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } }
OrderPizza2
public class OrderPizza2 { Pizza pizza = null; String orderType = ""; // 构造器 public OrderPizza2() { do { orderType = getType(); pizza = SimpleFactory.createPizza2(orderType); // 输出pizza if (pizza != null) { // 订购成功 pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } else { System.out.println(" 订购披萨失败 "); break; } } while (true); } // 写一个方法,可以获取客户希望订购的披萨种类 private String getType() { try { BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input pizza 种类:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } }
测试类 PizzaStore
//相当于一个客户端,发出订购 public class PizzaStore { public static void main(String[] args) { // TODO Auto-generated method stub //new OrderPizza(); //使用简单工厂模式 new OrderPizza(new SimpleFactory()); //System.out.println("~~退出程序~~"); //new OrderPizza2(); } }
工厂方法模式
介绍
需求2:
实现
BJCheesePizza
public class BJCheesePizza extends Pizza { @Override public void prepare() { // TODO Auto-generated method stub setName("北京的奶酪pizza"); System.out.println(" 北京的奶酪pizza 准备原材料"); } }
BJPepperPizza
public class BJPepperPizza extends Pizza { @Override public void prepare() { // TODO Auto-generated method stub setName("北京的胡椒pizza"); System.out.println(" 北京的胡椒pizza 准备原材料"); } }
LDCheesePizza
public class LDCheesePizza extends Pizza{ @Override public void prepare() { // TODO Auto-generated method stub setName("伦敦的奶酪pizza"); System.out.println(" 伦敦的奶酪pizza 准备原材料"); } }