alexpdh's blog

Java设计模式(一):简单工厂模式

简单工厂模式(simple factory pattern)

简单工厂模式(simple factory pattern):属于对象的创建方式。就是说我们考虑使用一个单独的类来处理类的实例化。例如,对于一个计算器,只需要考虑输入数字和运算符工厂就可以实例化出适合的对象计算出结果。


简单工厂模式UML图

simplefactorpattern


示例代码

运算类 Operation:用于计算结果。申明两个变量 numberA、numberB 用于记录需要计算的两个数,定义一个getResult()方法,用于获取计算结果。

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
package com.example.simpleFactoryPattern;
/**
* 运算类
*
* @author pengdh
* @date: 2017-03-15 22:44
*/
public class Operation {
protected double numberA;
protected double numberB;
public double getNumberA() {
return numberA;
}
public void setNumberA(double numberA) {
this.numberA = numberA;
}
public double getNumberB() {
return numberB;
}
public void setNumberB(double numberB) {
this.numberB = numberB;
}
public double getResult() {
double result = 0;
return result;
}
}

加法运算类 OperationAdd:继承运算类,用于加法运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.simpleFactoryPattern;
/**
* 加法运算类
*
* @author pengdh
* @date: 2017-03-15 23:19
*/
public class OperationAdd extends Operation {
@Override
public double getResult() {
double result = 0;
result = numberA + numberB;
return result;
}
}

减法运算类 OperationSub:继承运算类,用于减法运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.simpleFactoryPattern;
/**
* 减法运算类
*
* @author pengdh
* @date: 2017-03-15 23:24
*/
public class OperationSub extends Operation {
@Override
public double getResult() {
double result = 0;
result = numberA - numberB;
return result;
}
}

乘法运算类 OperationMul:继承运算类,用于乘法运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.simpleFactoryPattern;
/**
* 乘法运算类
*
* @author pengdh
* @date: 2017-03-15 23:27
*/
public class OperationMul extends Operation {
@Override
public double getResult() {
double result = 0;
result = numberA * numberB;
return result;
}
}

除法运算类 OperationDiv:继承运算类,用于除法运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.simpleFactoryPattern;
/**
* 除法运算类
*
* @author pengdh
* @date: 2017-03-15 23:30
*/
public class OperationDiv extends Operation {
@Override
public double getResult() {
double result = 0;
result = numberA / numberB;
return result;
}
}

工厂类 OperationFactory:根据输入的运算符,通过多态来创建适合的计算对象实现计算结果。

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
package com.example.simpleFactoryPattern;
/**
* 运算工厂类
*
* @author pengdh
* @date: 2017-03-15 23:35
*/
public class OperationFactory {
public static Operation createOperate(String operate) {
Operation oper = null;
switch (operate) {
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
}

客户端测试类 OperationTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.simpleFactoryPattern;
/**
* 运算测试类
*
* @author pengdh
* @date: 2017-03-16 0:16
*/
public class OperationTest {
public static void main(String[] args) {
Operation oper = new Operation();
oper = OperationFactory.createOperate("+");
oper.numberA = 3;
oper.numberB = 1;
double result = oper.getResult();
System.out.println("-----------pengdh----------- result = " + result + "," + "当前类 = OperationTest.main()");
}
}

以上通过 OperationFactory 工厂类来管理计算对象。这样如果有新的计算方法就不用再去原有的计算对象。例如,新加一个乘方运算,只需要新增一个继承运算类的乘方运算类,然后在工厂类新增一个乘方分支就可以了。


参考文献

alexpdh wechat
欢迎扫一扫关注 程序猿pdh 公众号!