23种设计模式之python实现--Prototype模式
2014-04-02 17:44
323 查看
#理解 #原型与工厂模式的区别应该是少一个与与产品平行的工厂类,通过克隆自己,可以创造多个对象 #例子 #有一个哺乳动物的原型,每个动物有名称, #人有很多,所以有很多Object,我们通过clone得到人的对象,然后在附上不同属性 #猴子也有很多,我们也可以通过clone得到多个猴子 class Prototype(object): typename='mammalian' def __init__(self): pass def clone(self): pass class Person(Prototype): def __init__(self): self.hair='black' self.Name='Person' Prototype.__init__(self)#调用父类初始化函数的一种方法 def clone(self): return Person() class monkey(Prototype): def __init__(self): self.hair='red' self.Name='monkey' super(Prototype,self).__init__()#调用父类初始化的另一种方法 def clone(self): return monkey() #client if __name__=="__main__": p=Person() p1=p.clone() print p print p.typename,p.hair,p.Name print p1 print p1.typename,p1.hair,p.Name Monkey=monkey() Monkey1=Monkey.clone() print Monkey print Monkey.typename,Monkey.hair,Monkey.Name print Monkey1 print Monkey.typename,Monkey1.hair,Monkey.Name
运行如图
相关文章推荐
- 23种设计模式之python实现--Decorator模式
- 23种设计模式之python实现--State模式
- 23种设计模式之python实现--抽象工厂
- 23种设计模式之python实现--Composite模式
- 23种设计模式之python实现--解释器模式
- 23种设计模式之python实现--Flyweight模式
- 23种设计模式之python实现--Adapter模式
- 23种设计模式之python实现--visitor模式
- 23种设计模式之python实现--Template模式
- 23种设计模式之python实现--中介者模式
- 23种设计模式之python实现
- 23种设计模式之python实现--单例模式
- 23种设计模式之python实现--builder模式
- 23种设计模式之python实现--Strategy模式
- 23种设计模式之python实现--command模式
- 23种设计模式之python实现--Facade模式
- 23种设计模式之python实现--职责链模式
- Python如何实现单例模式?其他23种设计模式python如何实现?
- 23种设计模式之python实现--观察者模式
- 23种设计模式之python实现--Iterator模式