言成言成啊 | Kit Chen's Blog

java设计模式

2019-10-17

需求

点披萨,可以点不同种类的披萨

比方说奶酪披萨,希腊披萨等等

然后有披萨的准备过程,烘烤过程,切片过程,打包过程

一、传统模式

  • 传统的方式
  • 优点:比较好理解,简单易操作
  • 缺点:违反了设计模式的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;

/*
* 将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() {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
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;//订购pizza的类型
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,制作过程
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}while(true);
}


//写一个方法,可以动态获取pizza类型
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) {
// TODO Auto-generated catch block
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

1
同传统模式

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() {
// TODO Auto-generated method stub
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);

// 输出pizza
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("订购披萨失败");
break;
}
} while (true);
}
// 写一个方法,可以动态获取pizza类型
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) {
// TODO Auto-generated catch block
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;

/*
* 简单(静态)工厂类
* 也叫静态工厂模式,需要把方法改成static,这样这个方法就直接可以通过类名调用了
*/
public class SimpleFactory {

//根据orderType返回对应的Pizza对象
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 OrderPizza();
//使用简单工厂模式
// new OrderPizza(new SimpleFactory());
//使用静态工厂模式,其实也是简单工厂,只不过方法是静态的
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;

/*
* 将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() {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
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 {
// 定义一个抽象方法,createPizza,让各个工厂子类,自己实现
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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

1
同工厂方法模式

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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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;


//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) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated catch block
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("欢迎下次使用,爱你么么哒❤❤");
}
}

运行结果

阅读量