Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
python 设计模式一
Experiences and Insight
929
views ·
5
replies ·
To
floor
Go
z85525006
deepin
2012-06-02 18:55
Author
由于找了很久,都找不到关于python的设计模式的相关书籍,就连算法和数据结构也是,
所以就将C#的设计模式转换过来.
这个例子是写了一个简单的例子,计算器.
# coding:utf-8
# 计算器
# 暴风
# 邮箱:
[email protected]
class Operation(object):
def __init__(self):
self.__numberA = 0.0
self.__numberB = 0.0
# NumberA.
@property
def NumberA(self):
return self.__numberA
@NumberA.setter
def NumberA(self, value):
self.__numberA = value
@NumberA.getter
def NumberA(self):
return self.__numberA
@NumberA.deleter
def NumberA(self):
del self.__numberA
# NumberB
@property
def NumberB(self):
return self.__numberB
@NumberB.setter
def NumberB(self, value):
self.__numberB = value
@NumberB.getter
def NumberB(self):
return self.__numberB
@NumberB.deleter
def NumberB(self):
del self.__numberB
class Add(Operation):
def __init__(self):
Operation.__init__(self)
def GetResult(self):
result = 0.0
result = self.NumberA + self.NumberB
return result
if __name__ == "__main__":
add = Add()
add.NumberA = 30
add.NumberB = 60
print add.GetResult()
Copy the Code
简单的工厂模式.有不好的地方还请大家多多指教.
# coding:utf-8
# 计算器
# 暴风
# 邮箱:
[email protected]
class Operation(object):
def __init__(self):
self.__numberA = 0.0
self.__numberB = 0.0
# NumberA.
@property
def NumberA(self):
return self.__numberA
@NumberA.setter
def NumberA(self, value):
self.__numberA = value
@NumberA.getter
def NumberA(self):
return self.__numberA
@NumberA.deleter
def NumberA(self):
del self.__numberA
# NumberB
@property
def NumberB(self):
return self.__numberB
@NumberB.setter
def NumberB(self, value):
self.__numberB = value
@NumberB.getter
def NumberB(self):
return self.__numberB
@NumberB.deleter
def NumberB(self):
del self.__numberB
class Add(Operation):
def __init__(self):
Operation.__init__(self)
def GetResult(self):
result = 0.0
result = self.NumberA + self.NumberB
return result
class Sub(Operation):
def __init__(self):
Operation.__init__(self)
def GetResult(self):
result = 0.0
result = self.NumberA - self.NumberB
return result
# 简单的工厂模式
class OperationFactory(object):
def createOperate(self, operate):
if "+" == operate:
return Add()
elif "-" == operate:
return Sub()
# elif "*" == operate:
# elif "/" == operate:
if __name__ == "__main__":
operation = Operation()
operation = OperationFactory().createOperate("+")
operation.NumberA = 30
operation.NumberB = 60
print operation.GetResult()
operation = OperationFactory().createOperate("-")
operation.NumberA = 30
operation.NumberB = 60
print operation.GetResult()
Copy the Code
设计模式能让代码复用,更容易维护.现在我要修改加法类,或者增加其它 除法类,...都很方便,而不用改一大堆的代码.
所以python上我也要好好的发挥设计模式的威力,只能下个月开始使用 设计模式啦.
Reply
Like 0
Favorite
View the author
All Replies
z85525006
deepin
2012-06-02 19:16
#1
下一节是 策策略模式, 简单的说就是对 算法的封装,
算法只是一种策略.
Reply
Like 0
View the author
cxbii
deepin
2012-06-02 19:20
#2
嗯嗯,围观之力顶
Reply
Like 0
View the author
cnchanghai
deepin
2012-06-02 23:39
#3
不懂,看气势挺NB
Reply
Like 0
View the author
jhkwei
deepin
2012-06-04 03:10
#4
四人帮的书,看过,不过差不多都忘记了。。
Reply
Like 0
View the author
a287740928
deepin
2012-06-04 04:19
#5
不懂,非专业,只是略懂c++和vb,哈哈
Reply
Like 0
View the author
Please
sign
in first
New Thread
Popular Events
More
所以就将C#的设计模式转换过来.
这个例子是写了一个简单的例子,计算器.
简单的工厂模式.有不好的地方还请大家多多指教.
所以python上我也要好好的发挥设计模式的威力,只能下个月开始使用 设计模式啦.