Python 之抽象工厂模式

简介:抽象工厂模式(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

总结

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

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。通常我们所说的工厂模式是指工厂方法模...
    zfylin阅读 5,110评论 0 7
  • 工厂方法模式通过引入工厂等级结构,解决了简单工厂模式中工厂类职责太重的问题,但由于工厂方法模式中的每个工厂只生产一...
    justCode_阅读 4,913评论 1 6
  • 一、介绍 意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。主要解决:主要解决接口选择的问...
    聂叼叼阅读 1,098评论 0 1
  • Bash 中的位置参数是由除 0 以外的一个或多个数字表示的参数。 位置参数是当 Shell 或 Shell 的函...
    赵者也阅读 5,929评论 0 0
  • 冬天渐渐来了,空气里温度骤然下降,路人们都穿着逐渐变厚的衣服,他们习惯地在人潮拥挤中走上公交车上的台阶上。 ...
    浅流苏阅读 767评论 0 0