抽象工廠模式 —— Python 描述

抽象工廠模式的實質是提供「接口」,子類經過實現這些接口來定義具體的操做。python

這些通用的接口如同協議同樣,協議自己定義了一系列方法去描述某個類,子類經過實現這些方法從而實現了該類。spa

子類中不用關心這個類該是什麼樣子的,這些都有抽象類去定義,這就區分設計類和實現類兩個過程,實現過程的解耦。設計

代碼描述

描述一個這樣的過程:有一個 GUIFactory,GUI 有 Mac 和 Windows 兩種風格,如今須要建立兩種不一樣風格的 Button,顯示在 Application 中。code

class GUIFactory(object):
    def __init__(self):
        self.screen = None

    def set_screen(self):
        raise NotImplementedError

    def get_size_from_screen(self, screen):
        if self.screen == 'retina':
            return '2560 x 1600'

        elif self.screen == 'full_hd':
            return '1920 x 1080'複製代碼

定義 Mac 和 Windows 兩種風格的 GUI。orm

class MacGUIFactory(GUIFactory):
    def __init__(self):
        super(MacGUIFactory, self).__init__()

    def set_screen(self):
        self.screen = 'retina' 

    def set_attributes(*args, **kwargs):
        raise NotImplementedError


class WinGUIFactory(GUIFactory):
    def __init__(self):
        super(WinGUIFactory, self).__init__()

    def set_screen(self):
        self.screen = 'full_hd' 

    def set_attributes(*args, **kwargs):
        raise NotImplementedError複製代碼

全部 GUI 都須要設置屏幕的類型,不然在調用時候會拋出 NotImplementedError 的異常,抽象了設置 Button 屬性的方法,讓子類必須實現。 接口

class MacButton(MacGUIFactory):
    def __init__(self):
        super(MacButton, self).__init__()
        self.set_screen()
        self.color = 'black'

    def set_attributes(self, *args, **kwargs):
        if 'color' in kwargs:
            self.color = kwargs.get('color') 

    def verbose(self):
        size = self.get_size_from_screen(self.screen) 
        print('''i am the {color} button of mac on {size} screen'''.format(
            color=self.color, size=size))


class WinButton(WinGUIFactory):
    def __init__(self):
        super(WinButton, self).__init__()
        self.set_screen()
        self.color = 'black'

    def set_attributes(self, *args, **kwargs):
        if 'color' in kwargs:
            self.color = kwargs.get('color') 

    def verbose(self):
        size = self.get_size_from_screen(self.screen) 
        print('''i am the {color} button of win on {size} screen'''.format(
            color=self.color, size=size))複製代碼

實現建立不一樣平臺 Button 的方法, 須要根據屏幕的大小建立不一樣尺寸的 Button,纔能有更好的顯示效果。ip

這樣就分別實現了兩種不一樣 Button 的建立,還能夠封裝一個 Button 類來管理這兩個不一樣的 Button。get

class Button(object):
 @staticmethod
    def create(platform, *args, **kwargs):
        if platform.lower() == 'mac':
            return MacButton()        
        elif platform.lower() == 'win':
            return WinButton()複製代碼

建立兩個不一樣平臺的 Button,而且設置顏色屬性string

win_button = Button.create('win')
mac_button = Button.create('mac')

win_button.set_attributes(color='red')
mac_button.set_attributes(color='blue')

win_button.verbose()
# >> i am the red button of win on 1920 x 1080 screen
mac_button.verbose()
# >> i am the blue button of mac on 2560 x 1600 screen複製代碼

適用範圍

如下狀況能夠適用抽象工廠模式:產品

  • 獨立於系統的模塊
  • 系統中各大類的模塊
  • 須要強調設計和實現分離的時候
  • 只想顯示接口並不想實現的時候

優缺點

優勢:

  • 具體產品從客戶代碼中被分離出來
  • 容易改變產品的系列
  • 將一個系列的產品族統一到一塊兒建立

缺點:

  • 在產品族中擴展新的產品是很困難的,它須要修改抽象工廠的接口

Reference

相關文章
相關標籤/搜索