简单工厂模式和工厂方法模式(未完成)

简单工厂模式和工厂方法模式

1.1 简单工厂模式

  简单工厂模式(Factory Method)

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//运算抽象类
public class Operation {
private double _numberA = 0;
private double _numberB = 0;

public double getResult(){
return 0;
}

public double get_numberA() {
return _numberA;
}

public void set_numberA(double _numberA) {
this._numberA = _numberA;
}

public double get_numberB() {
return _numberB;
}

public void set_numberB(double _numberB) {
this._numberB = _numberB;
}
}

//加法运算
public class OperationAdd extends Operation {
@Override
public double getResult() {
return get_numberA() + get_numberB();
}
}

......

//除法运算
public class OperationDiv extends Operation {
@Override
public double getResult() {
if(get_numberB() == 0)
throw new RuntimeException("除数禁止为0");
return get_numberA() / get_numberB();
}
}

/**
* 简单工厂模式实现计算器
*/
public class OperationFactory {
public static Operation createOperation(String operate) {
Operation operation = null;
switch (operate){
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
}
return operation;
}
}

public class Test {
public static void main(String[] args) {
//简单工厂模式
Operation operation = OperationFactory.createOperation("+");
operation.set_numberA(1);
operation.set_numberB(2);
double result = operation.getResult();
System.out.println("result = " + result);
}
}

1.2 工厂方法模式

  工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

  • Product,定义工厂方法所创建的对象的接口,即Operation。
  • ConcreteProduct,具体的产品,实现了Product接口,即OperationAdd等。
  • Creator,声明工厂方法,该方法返回一个Product类型的对象,即IFactory。
  • ConcreteCreator,重定义工厂方法以返回一个ConcreteProduct实例,即AddFactory等。

工厂方法模式结构图

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
//工厂接口
public interface IFactory {
Operation createOperation();
}

public class AddFactory implements IFactory {
@Override
public Operation createOperation() {
return new OperationAdd();
}
}

......

public class DivFactory implements IFactory {
@Override
public Operation createOperation() {
return new OperationDiv();
}
}

public class Test {
public static void main(String[] args) {
//工厂方法模式
IFactory operFactory = new AddFactory();
Operation oper = operFactory.createOperation();
oper.set_numberA(1);
oper.set_numberB(2);
double result1 = oper.getResult();
System.out.println("result1 = " + result1);
}
}

简单工厂和工厂方法的区别:简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。但这样的设计违背了开放-封闭原则,一旦需要增加功能,就要修改工厂增加分支。而工厂方法模式则不需要变更原有的工厂类,只需要增加功能对象的ConcreteProduct和ConcreteCreator即可。

  工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断转移到了客户端代码来进行,当我们想要加功能时,本来是改工厂类,现在是修改客户端。


参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》