設計模式Python版系列介紹:3、抽象工廠模式

設計模式Python版系列介紹:3、抽象工廠模式

抽象工廠模式(Abstract Factory Pattern)是圍繞一個超級工廠創建其他工廠。該超級工廠又稱為其他工廠的工廠。這種類型的設計模式屬於創建型模式,它提供了一種創建對象的最佳方式。

在抽象工廠模式中,接口是負責創建一個相關對象的工廠,不需要顯式指定它們的類。每個生成的工廠都能按照工廠模式提供對象。

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

缺點:產品族擴展非常困難,要增加一個系列的某一產品,既要在抽象的 Creator 里加代碼,又要在具體的裡面加代碼。

使用場景: 1、QQ 換皮膚,一整套一起換。 2、生成不同操作系統的程序。


UML類圖

抽象工廠在是工廠模式的基礎上,有增加了對工廠的抽象。

設計模式Python版系列介紹:3、抽象工廠模式

Python代碼實現

形狀類的實現

<code># -*- coding:utf-8 -*-import math# 1 先定義形狀類這個系列# 定義一個“形狀”的接口,裡面定義一個面積的接口方法,只有方法的定義,並沒有實現體class IShape(object):    def Area(self):        pass# 定義4個圖形類,都是實現Ishape接口,並且每一個圖形都有一個可以計算面積的方法,相當於重寫接口方法class Circle(IShape):    def Area(self, radius):        return math.pow(radius, 2) * math.piclass Rectangle(IShape):    def Area(self, longth, width):        return 2 * longth * widthclass Triangle(IShape):    def Area(self, baselong, height):        return baselong * height / 2class Ellipse(IShape):    def Area(self, long_a, short_b):        return long_a * short_b * math.pi/<code>

顏色類的實現

<code># 2 再定義顏色類這個系列# 定義一個“顏色”的接口,裡面定義一個顏色名稱的接口方法,只有方法的定義,並沒有實現體class IColor(object):    def color(self):        pass# 定義3個顏色類,都是實現IColor接口,並且每一個圖形都有一個可以獲取顏色名稱的方法,相當於重寫接口方法class Red(IColor):    def color(self, name):        print(f'我的顏色是:{name}')class Blue(IColor):    def color(self, name):        print(f'我的顏色是:{name}')class Black(IColor):    def color(self, name):        print(f'我的顏色是:{name}')/<code>

工廠類的實現:

<code># 3 定義抽象工廠以及與每一個系列對應的工廠class IFactory:  # 模擬接口    def create_shape(self):  # 定義接口的方法,只提供方法的聲明,不提供方法的具體實現        pass    def create_color(self):        pass# 創建形狀這一個系列的工廠class ShapeFactory(IFactory):  # 模擬類型實現某一個接口,實際上是類的繼承    def create_shape(self, name):  # 重寫接口中的方法        if name == 'Circle':            return Circle()        elif name == 'Rectangle':            return Rectangle()        elif name == 'Triangle':            return Triangle()        elif name == 'Ellipse':            return Ellipse()        else:            return None# 創建顏色這一個系列的工廠class ColorFactory(IFactory):  # 模擬類型實現某一個接口,實際上是類的繼承    def create_color(self, name):  # 重寫接口中的方法        if name == 'Red':            return Red()        elif name == 'Blue':            return Blue()        elif name == 'Black':            return Black()        else:            return None# 4 定義產生工廠類的類——抽象工廠模式的核心所在# 定義一個專門產生工廠的類class FactoryProducer:    def get_factory(self,name):        if name=='Shape':            return ShapeFactory()        elif name=='Color':            return ColorFactory()        else:            return None/<code> 

測試

<code>if __name__ == '__main__':    factory_producer = FactoryProducer()    shape_factory = factory_producer.get_factory('Shape')    color_factory = factory_producer.get_factory('Color')    # --------------------------------------------------------------    circle = shape_factory.create_shape('Circle')    circle_area = circle.Area(2)    print(f'這是一個圓,它的面積是:{circle_area}')    rectangle = shape_factory.create_shape('Rectangle')    rectangle_area = rectangle.Area(2, 3)    print(f'這是一個長方形,它的面積是:{rectangle_area}')    triangle = shape_factory.create_shape('Triangle')    triangle_area = triangle.Area(2, 3)    print(f'這是一個三角形,它的面積是:{triangle_area}')    ellipse = shape_factory.create_shape('Ellipse')    ellipse_area = ellipse.Area(3, 2)    print(f'這是一個橢圓,它的面積是:{ellipse_area}')    # ---------------------------------------------------------------    red = color_factory.create_color('Red')    red.color('紅色')    blue = color_factory.create_color('Blue')    blue.color('藍色')    black = color_factory.create_color('Black')    black.color('黑色')/<code>

運行結果:

<code>這是一個圓,它的面積是:12.566370614359172這是一個長方形,它的面積是:12這是一個三角形,它的面積是:3.0這是一個橢圓,它的面積是:18.84955592153876我的顏色是:紅色我的顏色是:藍色我的顏色是:黑色/<code>

工廠類與抽象工廠類的區別


設計模式Python版系列介紹:3、抽象工廠模式


分享到:


相關文章: