工厂设计模式----python版本
生活随笔
收集整理的這篇文章主要介紹了
工厂设计模式----python版本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#工廠設計模式
'''
date:2016/8/21
'''
#形狀接口
class Shape(object):def __init__(self):pass
def draw(self):pass
#長方形
class Retangle(Shape):def draw(self):print "Retangle..."
return "Retangle..."
#正方形
class Square(Shape):def draw(self):print "Square..."
return "Square..."
#獲取形狀的工廠
class ShapeFactory():def getShape(self,name):if name == None:return None
elif "Retangle" in name:return Retangle()elif "Square" in name:return Square()else:return None
if __name__ == '__main__':#1.獲取工廠
ShapeFactory=ShapeFactory()#2.從工廠中獲取對象
shape=ShapeFactory.getShape("Square")#3.運行方法
shape.draw()#運行結果:Square...
總結
以上是生活随笔為你收集整理的工厂设计模式----python版本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式----python版本
- 下一篇: xpath语法规范