Java设计模式-简单工厂模式
- 2020 年 3 月 17 日
- 筆記
简单工厂模式的基本介绍
1)简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中简单使用的模式
2)简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)
3)在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式
需求说明
1)披萨的种类:如CheesePizza,GreekPizza 2)披萨的制作过程包括:prepare、bake、cut、box 3)完成披萨店订购功能
传统方式实现
UML图
代码
// 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类继承抽象类Pizza 并实现prepare()方法 public class CheesePizza extends Pizza { @Override public void prepare() { System.out.println("CheesePizza 准备原材料"); } }
// GreekPizza类继承抽象类Pizza 并实现prepare()方法 public class GreekPizza extends Pizza{ @Override public void prepare() { System.out.println("GreekPizza 准备原材料"); } }
// OrderPizza 分别依赖Pizza类、GreekPizza类、CheesePizza类, public class OrderPizza { public OrderPizza(){ Pizza pizza = null; String orderType; do{ orderType = getType(); if(orderType.equals("greek")){ pizza = new GreekPizza(); pizza.setName("GreekPizza"); }else if(orderType.equals("cheese")){ pizza = new CheesePizza(); pizza.setName("CheesePizza"); }else { break; } 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 ""; } } }
// 程序的入口,运行程序 public class PizzaStore { public static void main(String[] args) { new OrderPizza(); } } // 运行结果 input pizza: greek GreekPizza 准备原材料 GreekPizza baking; GreekPizza cutting; GreekPizza boxing; input pizza: cheese CheesePizza 准备原材料 CheesePizza baking; CheesePizza cutting; CheesePizza boxing; input pizza:
传统方式的优缺点
1)优点是比较好理解,简单易操作 2)缺点是违反了设计模式的ocp原则,即对扩展开放,对修改关闭。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码
简单工厂模式实现
UML图
代码
// 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类继承抽象类Pizza 并实现prepare()方法 public class CheesePizza extends Pizza { @Override public void prepare() { System.out.println("CheesePizza 准备原材料"); } }
// GreekPizza类继承抽象类Pizza 并实现prepare()方法 public class GreekPizza extends Pizza{ @Override public void prepare() { System.out.println("GreekPizza 准备原材料"); } }
// SimpleFactory 类 public class SimpleFactory { public Pizza CreatePizza(String orderType){ Pizza pizza = null; System.out.println("使用简单工厂模式"); if (orderType.equals("greek")){ pizza = new GreekPizza(); pizza.setName("GreekPizza "); }else if(orderType.equals("cheese")){ pizza = new CheesePizza(); pizza.setName("CheesePizza "); } return pizza; } }
// OrderPizza类 public class OrderPizza01 { 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 ""; } } SimpleFactory simpleFactory; Pizza pizza = null; public OrderPizza01(SimpleFactory simpleFactory){ setFactory(simpleFactory); } public void setFactory(SimpleFactory simpleFactory){ String orderType; this.simpleFactory = simpleFactory; do{ orderType = getType(); pizza = this.simpleFactory.CreatePizza(orderType); if(pizza != null){ pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); }else { System.out.println("sorry"); break; } }while (true); } }
// 程序的入口,运行程序 public class PizzaStore { public static void main(String[] args) { new OrderPizza01(new SimpleFactory()); System.out.println("退出程序"); } } // 运行结果 input pizza: greek 使用简单工厂模式 GreekPizza 准备原材料 GreekPizza baking; GreekPizza cutting; GreekPizza boxing; input pizza: cheese 使用简单工厂模式 CheesePizza 准备原材料 CheesePizza baking; CheesePizza cutting; CheesePizza boxing; input pizza: hh 使用简单工厂模式 sorry 退出程序