美文网首页
Python 之抽象工厂模式

Python 之抽象工厂模式

作者: 极客匠 | 来源:发表于2020-01-31 19:51 被阅读0次

简介:抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。抽象工厂模式属于创建型模式。在抽象工厂模式中,接口负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。需要扩展时,只需添加对应的子工厂即可。

特性

提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。系统的产品有多余一个的产品族,而系统只消费其中某一族的产品。产品族难扩展,产品等级易扩展

优点

  • 当一个产品族的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族的对象

缺点

  • 产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的Creater里添加代码,又要在具体的里面加代码。

应用场景

  • 在一个产品族里面,定义了多个产品。例如:QQ换皮肤,一整套一起换
  • 生成不同的操作系统的程序

代码示例

我们将创建Shape和Color接口和实现这些接口的具体类。下一步是创建抽象工厂类AbstractFactory。接着定义工厂类ShapeFactory和ColorFactory,这两个工厂类都是扩展了AbstractFactory。然后创建一个工厂创造器类FactoryProducer。如下:

from abc import ABCMeta, abstractmethod


class Shape(object):
    _metaclass_ = ABCMeta

    @abstractmethod
    def draw(self):
        pass


class Rectangle(Shape):
    def draw(self):
        print("Inside Rectangle::draw() method.")


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


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


class Color(object):
    _metaclass_ = ABCMeta

    @abstractmethod
    def fill(self):
        pass


class Red(Color):
    def fill(self):
        print("Inside Red::draw() method.")


class Green(Color):
    def fill(self):
        print("Inside Green::draw() method.")


class Blue(Color):
    def fill(self):
        print("Inside Blue::draw() method.")


class AbstractFactory(object):
    _metaclass_ = ABCMeta

    @abstractmethod
    def getColor(self, color):
        pass

    @abstractmethod
    def getShape(self, shape):
        pass


class ShapeFactory(AbstractFactory):
    def getColor(self, color):
        pass

    def getShape(self, shape):
        if shape == "CIRCLE":
            return Circle()
        elif shape == "RECTANGLE":
            return Rectangle()
        elif shape == "SQUARE":
            return Square()


class ColorFactory(AbstractFactory):
    def getColor(self, color):
        if color == "RED":
            return Red()
        elif color == "GREEN":
            return Green()
        elif color == "BLUE":
            return Blue()

    def getShape(self, shape):
        pass


class FactoryProducer():
    @staticmethod
    def getFactory(choice):
        if choice == "SHAPE":
            return ShapeFactory()
        elif choice == "COLOR":
            return ColorFactory()


if __name__ == '__main__':
    shapeFactory1 = FactoryProducer.getFactory("SHAPE")
    shape1 = shapeFactory1.getShape("CIRCLE")
    shape1.draw()

    shape1 = shapeFactory1.getShape("RECTANGLE")
    shape1.draw()

    shapeFactory2 = FactoryProducer.getFactory("COLOR")
    shape2 = shapeFactory2.getColor("RED")
    shape2.fill()

    shape2 = shapeFactory2.getColor("GREEN")
    shape2.fill()

输出结果:

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Red::draw() method.
Inside Green::draw() method.

Process finished with exit code 0

总结

提供一个创建一系列相关或相互依赖对象的接口,当一个产品族的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族的对象。

每天多努力那么一点点,积少成多

相关文章

网友评论

      本文标题:Python 之抽象工厂模式

      本文链接:https://www.haomeiwen.com/subject/dpqnthtx.html