Python 之抽象工廠模式

簡介:抽象工廠模式(Abstract Factory Pattern)是圍繞一個超級工廠創(chuàng)建其他工廠。抽象工廠模式屬于創(chuàng)建型模式。在抽象工廠模式中,接口負責創(chuàng)建一個相關對象的工廠,不需要顯式指定它們的類。每個生成的工廠都能按照工廠模式提供對象。需要擴展時,只需添加對應的子工廠即可。

特性

提供一個創(chuàng)建一系列相關或相互依賴對象的接口,而無須指定它們具體的類。系統(tǒng)的產品有多余一個的產品族,而系統(tǒng)只消費其中某一族的產品。產品族難擴展,產品等級易擴展

優(yōu)點

  • 當一個產品族的多個對象被設計成一起工作時,它能保證客戶端始終只使用同一個產品族的對象

缺點

  • 產品族擴展非常困難,要增加一個系列的某一產品,既要在抽象的Creater里添加代碼,又要在具體的里面加代碼。

應用場景

  • 在一個產品族里面,定義了多個產品。例如:QQ換皮膚,一整套一起換
  • 生成不同的操作系統(tǒng)的程序

代碼示例

我們將創(chuàng)建Shape和Color接口和實現(xiàn)這些接口的具體類。下一步是創(chuàng)建抽象工廠類AbstractFactory。接著定義工廠類ShapeFactory和ColorFactory,這兩個工廠類都是擴展了AbstractFactory。然后創(chuàng)建一個工廠創(chuàng)造器類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

總結

提供一個創(chuàng)建一系列相關或相互依賴對象的接口,當一個產品族的多個對象被設計成一起工作時,它能保證客戶端始終只使用同一個產品族的對象。

每天多努力那么一點點,積少成多

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 工廠模式是我們最常用的實例化對象模式了,是用工廠方法代替new操作的一種模式。通常我們所說的工廠模式是指工廠方法模...
    zfylin閱讀 1,409評論 0 7
  • 工廠方法模式通過引入工廠等級結構,解決了簡單工廠模式中工廠類職責太重的問題,但由于工廠方法模式中的每個工廠只生產一...
    justCode_閱讀 1,300評論 1 6
  • 一、介紹 意圖:提供一個創(chuàng)建一系列相關或相互依賴對象的接口,而無需指定它們具體的類。主要解決:主要解決接口選擇的問...
    聶叼叼閱讀 193評論 0 1
  • Bash 中的位置參數(shù)是由除 0 以外的一個或多個數(shù)字表示的參數(shù)。 位置參數(shù)是當 Shell 或 Shell 的函...
    趙者也閱讀 2,016評論 0 0
  • 冬天漸漸來了,空氣里溫度驟然下降,路人們都穿著逐漸變厚的衣服,他們習慣地在人潮擁擠中走上公交車上的臺階上。 ...
    淺流蘇閱讀 149評論 0 0

友情鏈接更多精彩內容