工厂设计模式----python版本
#!/usr/bin/python# -*- coding: UTF-8 -*-#工厂设计模式'''date:2016/8/21'''#形状接口class Shape(object):def __init__(self):passdef draw(self):pass#长方形class Retangle(Shape):def...
·
#!/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...
更多推荐
已为社区贡献7条内容
所有评论(0)