需求
点披萨,可以点不同种类的披萨
比方说奶酪披萨,希腊披萨等等
然后有披萨的准备过程,烘烤过程,切片过程,打包过程
一、传统模式
- 传统的方式
- 优点:比较好理解,简单易操作
- 缺点:违反了设计模式的ocp原则,即对扩展开放,对修改关闭。
- 即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少得修改代码
- 比如,我要新增加一个pizza种类,需要在OrderPizza里面再加一个elseif,这样并不好
类图:
Pizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package factory.pizza;
public abstract class Pizza { private 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; }
public String getName() { return name; } }
|
CheesePizza.java
1 2 3 4 5 6 7 8 9 10 11
| package factory.pizza;
public class CheesePizza extends Pizza {
@Override public void prepare() { System.out.println("给制作CheesePizza准备原材料 "); }
}
|
GreekPizza.java
1 2 3 4 5 6 7 8 9 10 11
| package factory.pizza;
public class GreekPizza extends Pizza {
@Override public void prepare() { System.out.println("给制作GreekPizza准备原材料"); }
}
|
OrderPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package factory.order;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
import factory.pizza.CheesePizza; import factory.pizza.GreekPizza; import factory.pizza.Pizza;
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 { System.out.println("俺们家不做这个pizza,请去其他商店"); break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); }while(true); } public String getType() { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input pizza type:"); String type; try { type = input.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } return type; } }
|
PizzaStore.java
1 2 3 4 5 6 7 8 9
| package factory.order;
public class PizzaStore { public static void main(String[] args) { new OrderPizza(); } }
|
运行结果
二、简单(静态)工厂模式
- 简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
- 简单工厂模式是工厂模式家族中最简单实用的模式
- 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)
类图
Pizza.java&&GreekPizza.java&&CheesePizza.java
Chinese.java
1 2 3 4 5 6 7 8 9 10 11
| package factory.pizza;
public class ChinesePizza extends Pizza {
@Override public void prepare() { System.out.println("给制作GreekPizza准备原材料"); }
}
|
OrderPizza2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package factory.order;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import factory.pizza.Pizza;
public class OrderPizza2 {
SimpleFactory simpleFactory; Pizza pizza = null; public OrderPizza2() { String orderType = ""; do { orderType = getType(); pizza = SimpleFactory.createPizza(orderType);
if (pizza != null) { pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); }else { System.out.println("订购披萨失败"); break; } } while (true); } public String getType() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input pizza type:"); String type; try { type = input.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } return type; } }
|
SimpleFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package factory.order;
import factory.pizza.CheesePizza; import factory.pizza.ChinesePizza; import factory.pizza.GreekPizza; import factory.pizza.Pizza;
public class SimpleFactory { public static Pizza createPizza(String orderType) { System.out.println("使用简单工厂模式"); Pizza pizza=null; if(orderType.equals("greek")) { pizza=new GreekPizza(); pizza.setName("GreekPizza"); }else if(orderType.equals("cheese")) { pizza=new CheesePizza(); pizza.setName("CheesePizza"); }else if(orderType.equals("chinese")) { pizza=new ChinesePizza(); pizza.setName("ChinesePizza"); } return pizza; } }
|
PizzaStore.java
1 2 3 4 5 6 7 8 9 10 11
| public class PizzaStore { public static void main(String[] args) {
new OrderPizza2(); System.out.println("退出程序,爱你么么哒❤❤"); } }
|
运行结果
新需求
客户在点披萨的时候,可以点不同口味的披萨
比方说:北京
的奶酪
披萨,伦敦
的胡椒
披萨
思路1:
使用简单工厂模式,创建不同的简单工厂类,BJPizzaSimpleFactory,LDPizzaSimpleFactory等等
但是考虑到项目的规模,以及软件的可维护性、可扩展性并不是特别好
思路2:
使用工厂方法模式
工厂方法模式——定义一个创建对象的抽象方法,由子类决定要实例化的类。工厂方法模式将对象的实例化推迟到子类。
三、工厂方法模式
类图
Pizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package factory.factoryMethod.pizzaStore.pizza;
public abstract class Pizza { private 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; }
public String getName() { return name; } }
|
BJCheesePizza.java
1 2 3 4 5 6 7 8 9 10 11 12
| package factory.factoryMethod.pizzaStore.pizza;
public class BJCheesePizza extends Pizza {
@Override public void prepare() { setName("BJCheesePizza"); System.out.println("BJCheesePizza准备原材料"); }
}
|
BJPepperPizza.java
1 2 3 4 5 6 7 8 9 10 11 12
| package factory.factoryMethod.pizzaStore.pizza;
public class BJPepperPizza extends Pizza {
@Override public void prepare() { setName("BJPepperPizza"); System.out.println("BJPepperPizza准备原材料"); }
}
|
LDCheesePizza.java
1 2 3 4 5 6 7 8 9 10 11 12
| package factory.factoryMethod.pizzaStore.pizza;
public class LDCheesePizza extends Pizza {
@Override public void prepare() { setName("LDCheesePizza"); System.out.println("LDCheesePizza准备原材料"); }
}
|
LDPepperPizza.java
1 2 3 4 5 6 7 8 9 10 11 12
| package factory.factoryMethod.pizzaStore.pizza;
public class LDPepperPizza extends Pizza {
@Override public void prepare() { setName("LDPepperPizza"); System.out.println("LDPepperPizza准备原材料"); }
}
|
OrderPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package factory.factoryMethod.pizzaStore.order;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
import factory.factoryMethod.pizzaStore.pizza.Pizza;
public abstract class OrderPizza { abstract Pizza createPizza(String orderType);
public OrderPizza() { Pizza pizza = null; String orderType = null; do { orderType = getType(); pizza = createPizza(orderType); if(pizza==null) break; pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); System.out.println("披萨已送到你家!!"); System.out.println(); } while (true); }
public String getType() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String orderType; System.out.println("Please input pizza's type:"); try { orderType = input.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } return orderType;
} }
|
BJOrderPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package factory.factoryMethod.pizzaStore.order;
import factory.factoryMethod.pizzaStore.pizza.BJCheesePizza; import factory.factoryMethod.pizzaStore.pizza.BJPepperPizza; import factory.factoryMethod.pizzaStore.pizza.Pizza;
public class BJOrderPizza extends OrderPizza {
@Override Pizza createPizza(String orderType) { Pizza pizza=null; if(orderType.equals("cheese")) { pizza=new BJCheesePizza(); }else if(orderType.equals("pepper")) { pizza=new BJPepperPizza(); } return pizza; }
}
|
LDOrderPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package factory.factoryMethod.pizzaStore.order;
import factory.factoryMethod.pizzaStore.pizza.LDCheesePizza; import factory.factoryMethod.pizzaStore.pizza.LDPepperPizza; import factory.factoryMethod.pizzaStore.pizza.Pizza;
public class LDOrderPizza extends OrderPizza {
@Override Pizza createPizza(String orderType) { Pizza pizza=null; if(orderType.equals("cheese")) { pizza=new LDCheesePizza(); }else if(orderType.equals("pepper")) { pizza=new LDPepperPizza(); } return pizza; }
}
|
运行结果
四、抽象工厂模式
基本介绍
- 抽象工厂模式:定义了一个
interface
用于创建相关或有依赖关系的对象簇,而无需指明具体的类 - 抽象工厂模式可以将
简单工厂模式
和工厂方法模式
进行整合 - 从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或者称为进一步的抽象)
- 将工厂抽象成两层,
AbsFactory(抽象工厂)
和具体实现的工厂子类
。程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更有利于代码的维护和扩展。
类图
Pizza.java、BJCheesePizza.java、BJPepperPizza.java、LDCheesePizza.java、LDPepperPizza.java
AbsFactory.java
1 2 3 4 5 6 7 8 9
| package factory.abstractFactory.pizzaStore.order;
import factory.abstractFactory.pizzaStore.pizza.Pizza;
public interface AbsFactory { public Pizza createPizza(String orderType); }
|
BJFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package factory.abstractFactory.pizzaStore.order;
import factory.abstractFactory.pizzaStore.pizza.BJCheesePizza; import factory.abstractFactory.pizzaStore.pizza.BJPepperPizza; import factory.abstractFactory.pizzaStore.pizza.Pizza;
public class BJFactory implements AbsFactory {
@Override public Pizza createPizza(String orderType) { Pizza pizza=null; if(orderType.equals("cheese")) { pizza=new BJCheesePizza(); }else if(orderType.equals("pepper")) { pizza=new BJPepperPizza(); } return pizza; }
}
|
LDFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package factory.abstractFactory.pizzaStore.order;
import factory.abstractFactory.pizzaStore.pizza.LDCheesePizza; import factory.abstractFactory.pizzaStore.pizza.LDPepperPizza; import factory.abstractFactory.pizzaStore.pizza.Pizza;
public class LDFactory implements AbsFactory {
@Override public Pizza createPizza(String orderType) { Pizza pizza=null; if(orderType.equals("cheese")) { pizza=new LDCheesePizza(); }else if(orderType.equals("pepper")) { pizza=new LDPepperPizza(); } return pizza; }
}
|
OrderPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package factory.abstractFactory.pizzaStore.order;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
import factory.abstractFactory.pizzaStore.pizza.Pizza;
public class OrderPizza { AbsFactory absFactory;
public void setAbsFactory(AbsFactory absFactory) { Pizza pizza=null; String orderType; this.absFactory=absFactory; do { orderType=getType(); pizza=absFactory.createPizza(orderType); if(pizza==null) { System.out.println("俺家不做这个披萨哦,请去别家吧❤❤"); break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); System.out.println("您订购的Pizza已经在路上了哟❤❤"); System.out.println(); }while(true); } public String getType() { String orderType; BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input pizza's type: pepper or cheese"); try { orderType=input.readLine(); } catch (IOException e) { e.printStackTrace(); orderType=""; } return orderType; } }
|
PizzaStore.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package factory.abstractFactory.pizzaStore.order;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class PizzaStore { public static void main(String[] args) { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input pizza's location:Beijing or London"); String loc; try { loc = input.readLine(); } catch (IOException e) { e.printStackTrace(); loc=""; } if(loc.equals("Beijing")) { new OrderPizza().setAbsFactory(new BJFactory()); }else if(loc.equals("London")) { new OrderPizza().setAbsFactory(new LDFactory()); } System.out.println("欢迎下次使用,爱你么么哒❤❤"); } }
|
运行结果