设计模式之-抽象工厂模式VS简单工厂模式(python)


从某种成度上说抽象工厂模式就是简单工厂模式的工厂模式。工厂模式使用依赖和继承产生新的对象,而抽象工厂模式跟多的是以组合方式产生新的类。

简单工厂模式的结构如下图:



简单工厂模式示例代码:

  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 _*__  
  3.   
  4. #implementation of the simple factory pattern   
  5. import random  
  6.   
  7. class Shape(object):  
  8.     # Create based on class name:  
  9.     def factory(type):  
  10.         #return eval(type + "()")  
  11.         if type == "Circle"return Circle()  
  12.         if type == "Square"return Square()  
  13.         assert 0"Bad shape creation: " + type  
  14.     factory = staticmethod(factory)  
  15.   
  16. class Circle(Shape):  
  17.     def draw(self): print("Circle.draw")  
  18.     def erase(self): print("Circle.erase")  
  19.   
  20. class Square(Shape):  
  21.     def draw(self): print("Square.draw")  
  22.     def erase(self): print("Square.erase")  
  23.   
  24. # Generate shape name strings:  
  25. def shapeNameGen(n):  
  26.     types = Shape.__subclasses__()  
  27.     for i in range(n):  
  28.         yield random.choice(types).__name__  
  29.   
  30. shapes = [ Shape.factory(i) for i in shapeNameGen(7)]  
  31.   
  32. for shape in shapes:  
  33.     shape.draw()  
  34.     shape.erase()  
#!/usr/bin/env python
# -*- coding: utf-8 _*__

#implementation of the simple factory pattern 
import random

class Shape(object):
    # Create based on class name:
    def factory(type):
        #return eval(type + "()")
        if type == "Circle": return Circle()
        if type == "Square": return Square()
        assert 0, "Bad shape creation: " + type
    factory = staticmethod(factory)

class Circle(Shape):
    def draw(self): print("Circle.draw")
    def erase(self): print("Circle.erase")

class Square(Shape):
    def draw(self): print("Square.draw")
    def erase(self): print("Square.erase")

# Generate shape name strings:
def shapeNameGen(n):
    types = Shape.__subclasses__()
    for i in range(n):
        yield random.choice(types).__name__

shapes = [ Shape.factory(i) for i in shapeNameGen(7)]

for shape in shapes:
    shape.draw()
    shape.erase()
抽象工厂模式的结构如下:


抽象工厂模式示例代码:

  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 _*__  
  3.   
  4. #implementation of the atbstract factory pattern   
  5.   
  6.   
  7. class AbstractFactory(object):  
  8.     def getShape(slef):  
  9.         return Shape()  
  10.   
  11.     def getColor(slef):  
  12.         return Color()  
  13.   
  14.   
  15. class Shape(AbstractFactory):  
  16.     #create  based on class name  
  17.     @staticmethod  
  18.     def shapeFactory(type):  
  19.         #return eval(type + "0")  
  20.         if type == "Circle":  
  21.             return Circle()  
  22.         if type == "Square":  
  23.             return Square()  
  24.         assert 0 , "Bad shape creation: " + type  
  25.           
  26.   
  27. class Circle(Shape):  
  28.     def __init__(self):  
  29.         self.name = "Circle"  
  30.   
  31.     def draw(self):  
  32.         print(self.name+" draw")  
  33.   
  34.     def erase(self):  
  35.         print(self.name+" erase")  
  36.   
  37.   
  38. class Square(Shape):  
  39.     def __init__(self):  
  40.         self.name = "Square"  
  41.   
  42.     def draw(self):  
  43.         print(self.name+' draw')  
  44.   
  45.     def erase(self):  
  46.         print(self.name+" erase")  
  47.   
  48.   
  49. class Color(AbstractFactory):  
  50.     #create  based on class name  
  51.     @staticmethod  
  52.     def colorFactory(type):  
  53.         #return eval(type + "0")  
  54.         if type == "Red":  
  55.             return Red()  
  56.         if type == "Green":  
  57.             return Green()  
  58.         assert 0 , "Bad color creation: " + type  
  59.   
  60.   
  61. class Red(Color):  
  62.     def __init__(self):  
  63.         self.name = "Red"  
  64.   
  65.     def padding(self):  
  66.         print(self.name+" padding")  
  67.   
  68.     def reset(self):  
  69.         print(self.name+" reset")  
  70.   
  71.   
  72. class Green(Color):  
  73.     def __init__(self):  
  74.         self.name = "Green"  
  75.   
  76.     def padding(self):  
  77.         print(self.name+" padding")  
  78.   
  79.     def reset(self):  
  80.         print(self.name+" reset")         
  81.   
  82.   
  83.   
  84.   
  85. if __name__ == '__main__':  
  86.     abstractFactory = AbstractFactory()  
  87.     types = Shape.__subclasses__()  
  88.     for type in types:  
  89.         circle = abstractFactory.getShape().shapeFactory(type.__name__)  
  90.         circle.draw()  
  91.         circle.erase()  
  92.     types = Color.__subclasses__()  
  93.     for type in types:  
  94.         color = abstractFactory.getColor().colorFactory(type.__name__)  
  95.         color.padding()  
  96.         color.reset()  
  97.   
  98.   
  99.   
  100.           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值