23种设计模式之python实现--抽象工厂
2014-04-01 20:49
726 查看
#理解 #Abstract_Factory模式是Factory的扩展, #Factory用于生产一类产品而抽象工厂用于生产多个产品系列 #例子: #两个工厂,一个生产狗玩具,一个生产猫玩具,而玩具共有两类一类机器玩具,一类毛绒玩具 class Abstract_Factory: def __init__(self,factory=None): self.toy_factory=factory class cat_Factory(Abstract_Factory): #当然这里可以象Factory_Method使用查表 def create_soft_cat(self): return soft_cat_toy(); def create_machine_cat(self): return machine_cat_toy() class dog_Factory(Abstract_Factory): def create_soft_dog(self): return soft_dog_toy(); def create_machine_dog(self): return machine_dog_toy(); class Abstract_Product_dog: def getname(self): pass def speak(self): print "wangwang" class soft_dog_toy(Abstract_Product_dog): def getname(self): return "soft dog toy" class machine_dog_toy(Abstract_Product_dog): def getname(self): return "machine dog toy" class Abstract_Product_cat: def getname(self): pass def speak(self): print "miaomiao" class soft_cat_toy(Abstract_Product_cat): def getname(self): return "soft cat toy" class machine_cat_toy(Abstract_Product_cat): def getname(self): return "machine cat toy" if __name__=="__main__": factory=Abstract_Factory(dog_Factory()); dog1=factory.toy_factory.create_soft_dog() dog1.speak() print dog1.getname() dog2=factory.toy_factory.create_machine_dog() dog2.speak() print dog2.getname() factory=Abstract_Factory(cat_Factory()) cat1=factory.toy_factory.create_soft_cat() cat1.speak() print cat1.getname() cat2=factory.toy_factory.create_machine_cat() cat2.speak() print cat2.getname()
运行结果,如图
相关文章推荐
- 23种设计模式之python实现--工厂方法
- java23种设计模式之工厂模式(Factory)的实现
- 23种设计模式之python实现--Iterator模式
- [设计模式之禅读书笔记]009_23种设计模式三:抽象工厂方法
- 设计模式之一:简单工厂模式,用python实现简易计算器
- 【23种设计模式】创建型模式 > 三个工厂模式(简单工厂、抽象工厂、工厂方法)
- 23种设计模式之python实现--Flyweight模式
- 设计模式之抽象工厂,C++实现(一)
- 23种设计模式之python实现--解释器模式
- 23种设计模式之python实现--State模式
- python实现的工厂方法--设计模式初级
- 设计模式之单例模式与工厂模式的Python实现(一)
- 23种设计模式之python实现--总结
- 23种设计模式之python实现--Memento模式
- 23种设计模式之python实现--中介者模式
- PHP实现抽象工厂设计模式
- 23种设计模式之python实现--Facade模式
- python实现工厂方法设计模式
- 23种设计模式-(AbstractFactory)抽象工厂
- 设计模式的C++实现 3.抽象工厂