大话设计模式——简单工厂模式

简单工厂模式的核心思想是通过创建一个工厂类来根据不同条件生产不同的类,然后根据不同类对基类方法的覆写得到不同的结果。

优势

新添加类时,不影响之前的代码,仅需编写新增类代码和修改工厂类代码。

劣势

客户端必须知道基类和工厂类,耦合性差。

程序实例

四则运算计算器,根据用户的输入生产对应的运算类并计算结果。

UML类图

C#代码

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public class Operation
{
private double _numberA = 0;
private double _numberB = 0;

public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
public virtual double GetResult()
{

double result = 0;
return result;
}
}

class OperationAdd : Operation
{
public override double GetResult()
{

double result = 0;
result = NumberA + NumberB;
return result;
}
}

class OperationSub : Operation
{
public override double GetResult()
{

double result = 0;
result = NumberA - NumberB;
return result;
}
}

class OperationMul : Operation
{
public override double GetResult()
{

double result = 0;
result = NumberA * NumberB;
return result;
}
}

class OperationDiv : Operation
{
public override double GetResult()
{

double result = 0;
if (NumberB == 0)
throw new Exception("除数不能为0.");
result = NumberA / NumberB;
return result;
}
}

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;
}
}

class Program
{
static void Main(string[] args)
{

Operation oper;
Console.Write("Operator: ");
oper = OPerationFactory.createOperate(Console.ReadLine());
Console.Write("NumberA: ");
oper.NumberA = double.Parse(Console.ReadLine());
Console.Write("NumberB: ");
oper.NumberB = double.Parse(Console.ReadLine());
double result = oper.GetResult();
Console.WriteLine("Result: " + result.ToString());
}
}

Python代码

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
# coding: utf-8
class Operation:
NumberA = 0
NumberB = 0
def GetResult(self):
pass

class OperationAdd(Operation):
def GetResult(self):
return self.NumberA + self.NumberB

class OperationSub(Operation):
def GetResult(self):
return self.NumberA - self.NumberB

class OperationMul(Operation):
def GetResult(self):
return self.NumberA * self.NumberB

class OperationDiv(Operation):
def GetResult(self):
if self.NumberB == 0:
raise Exception(u'除数不能为0.')
return self.NumberA / float(self.NumberB)

class OperationFactory(Operation):
operation = dict()
operation['+'] = OperationAdd()
operation['-'] = OperationSub()
operation['*'] = OperationMul()
operation['/'] = OperationDiv()

def createOperation(self, operate):
oper = None
if operate in self.operation:
oper = self.operation[operate]
return oper

if __name__ == '__main__':
oper = OperationFactory().createOperation(raw_input('Operator: '))
oper.NumberA = input('NumberA: ')
oper.NumberB = input('NumberB: ')
print 'Result:',oper.GetResult()