学习python有一年了,在工作中经常使用,不过多是一些基础操作和简单库的使用,受朋友打击,想学习一些深层次的内容,所以从本篇开始学习设计模式,首先是简单工厂模式。
其实很早就将设计模式作为学习过程中的一环,不过以前基础薄弱,想来用不到高深的设计模式,所以并没有去学习相关知识,感觉现在可以了就开始看书,买了两本书《设计模式:可复用面向对象软件的基础》和《大话设计模式》,目前主要是顺序阅读《大话设计模式》并将其中C#代码用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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sys

class OperationFactory(object):
''' factory class to generate operation function '''
def __init__(self, operation):
self.operation = operation

def createOperation(self):
oper = None
if self.operation == '+':
oper = OperationAdd()
elif self.operation == '-':
oper = OperationSub()
elif self.operation == '*':
oper = OperationMul()
elif self.operation == '/':
oper = OperationDiv()
elif self.operation == '**':
oper = OperationPow()
else:
raise Exception, 'No correct operation.'

return oper

class Operation(object):
''' base class operation '''
def __init__(self):
self.numA = 0
self.numB = 0
print 'Init Operation class', self.numA, self.numB

def getResult(self):
print 'Call Operation function', self.numA, self.numB
result = 0
return result

class OperationAdd(Operation):

def __init__(self):
Operation.__init__(self)
print 'Init OperationAdd class', self.numA, self.numB

def getResult(self):
print 'Call OperationAdd function', self.numA, self.numB
result = 0
result = self.numA + self.numB
return result

class OperationSub(Operation):

def __init__(self):
Operation.__init__(self)
print 'Init OperationSub class', self.numA, self.numB

def getResult(self):
print 'Call OperationSub function', self.numA, self.numB
result = 0
result = self.numA - self.numB
return result

class OperationMul(Operation):

def __init__(self):
Operation.__init__(self)
print 'Init OperationMul class', self.numA, self.numB

def getResult(self):
print 'Call OperationMul function', self.numA, self.numB
result = 0
result = self.numA * self.numB
return result

class OperationDiv(Operation):

def __init__(self):
Operation.__init__(self)
print 'Init OperationDiv class', self.numA, self.numB

def getResult(self):
print 'Call OperationDiv function', self.numA, self.numB
result = 0
try:
result = self.numA / self.numB
except ZeroDivisionError, e:
raise e
return result

class OperationPow(Operation):

def __init__(self):
Operation.__init__(self)
print 'Init OperationPow class', self.numA, self.numB

def getResult(self):
print 'Call OperationPow function', self.numA, self.numB
result = 0
result = self.numA ** self.numB
return result

def main():
if len(sys.argv) != 4:
print 'Please enter two number and one operation like 3 + 2'
sys.exit(-1)

numA = int(sys.argv[1])
# if cmd has *, it will search all files in current dir, so replace * with '*'
operation = sys.argv[2]
numB = int(sys.argv[3])

oper = OperationFactory(operation).createOperation()
oper.numA = numA
oper.numB = numB
result = oper.getResult()
print result

# test OperationAdd
# oper = OperationAdd()
# oper.numA = numA
# oper.numB = numB
# oper.getResult()
main()

测试结果:

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
$python simpleFactory.py
Please enter two number and one operation like 3 + 2

$python simpleFactory.py 3 + 2
Init Operation class 0 0
Init OperationAdd class 0 0
Call OperationAdd function 3 2
5

lisp@lisp-PC /cygdrive/h
$python simpleFactory.py 3 - 2
Init Operation class 0 0
Init OperationSub class 0 0
Call OperationSub function 3 2
1

lisp@lisp-PC /cygdrive/h
$python simpleFactory.py 3 '*' 2
Init Operation class 0 0
Init OperationMul class 0 0
Call OperationMul function 3 2
6

lisp@lisp-PC /cygdrive/h
$python simpleFactory.py 3 / 2
Init Operation class 0 0
Init OperationDiv class 0 0
Call OperationDiv function 3 2
1

lisp@lisp-PC /cygdrive/h
$python simpleFactory.py 3 '**' 2
Init Operation class 0 0
Init OperationPow class 0 0
Call OperationPow function 3 2
9