python-組合模式

來源:互聯網
上載者:User

標籤:nsf   img   抽象   man   結構   業務   ring   dev   依賴倒置   

說明:

組合模式主要用來處理一類具有“容器特徵”的對象——它們即充當對象又可以作為容器包含其他多個對象。組合模式對“容器特徵”的對象和單個對象一視同仁。例如在電腦處理算術運算式的時候,算術運算式包括運算元,操作符和另一個運算元。運算元可以是一個數字也可以是另一個運算式。這樣7+8和(2+3)+(4*6)都是合法的運算式,電腦先將(2+3)+(4*6)分解為(2+3)和(4*6)在對上述兩個運算元進行進一步分解。

組合模式也叫作部分-整體模式,其定義如下:將對象組合成樹形結構以表示“部分”和“整體”的階層,使得使用者對單個對象和組合對象的使用具有一致性。

 

執行個體:

每一個公司都有自己的組織圖,越是大型的企業,其組織圖就會越複雜。大多數情況下,公司喜歡用“樹形”結構來組織複雜的公司人事關係和公司間的結構關係。一般情況下,根結點代表公司的最高行政權利單位,分支節點表示一個個部門,而葉子結點則會用來代表每一個員工。每一個結點的子樹,表示該結點代表的部門所管理的單位。假設一個具有HR部門,財務部門和研發部門,同時在全國有分支公司的總公司,其公司結構,可以表示成如下邏輯:

class Company:    name = ‘‘    def __init__(self, name):        self.name = name    def add(self, company):        pass    def remove(self, company):        pass    def display(self, depth):        pass    def listDuty(self):        passclass ConcreteCompany(Company):    childrenCompany = None    def __init__(self, name):        Company.__init__(self,name)        self.childrenCompany = []    def add(self, company):        self.childrenCompany.append(company)    def remove(self, company):        self.childrenCompany.remove(company)    def display(self, depth):        print(‘-‘*depth + self.name)        for component in self.childrenCompany:            component.display(depth+1)    def listDuty(self):        for component in self.childrenCompany:            component.listDuty()class HRDepartment(Company):    def __init__(self, name):         Company.__init__(self,name)    def display(self, depth):        print(‘-‘*depth + self.name)    def listDuty(self): #履行職責        print(‘%s\t Enrolling & Transfering management.‘ % self.name)class FinanceDepartment(Company):    def __init__(self, name):        Company.__init__(self,name)    def display(self, depth):        print("-" * depth + self.name)    def listDuty(self): #履行職責        print(‘%s\tFinance Management.‘%self.name)class RdDepartment(Company):    def __init__(self,name):        Company.__init__(self,name)    def display(self, depth):        print("-"*depth+self.name)    def listDuty(self):        print("%s\tResearch & Development."% self.name)

"""
在該例中,公司結構抽象僅考慮公司(ConcreteCompany)和部門(Department),
公司有子公司的可能性,公司也有自己的部門,部門是最終的葉子結點。
假設總公司下設東邊的分公司一個,東邊的分公司下設東北公司和東南公司,顯示公司層級,
並羅列這些的公司中各部門的職責,可以構建如下業務情境:
"""if __name__=="__main__": root = ConcreteCompany(‘HeadQuarter‘) root.add(HRDepartment(‘HQ HR‘)) root.add(FinanceDepartment(‘HQ Finance‘)) root.add(RdDepartment("HQ R&D")) comp = ConcreteCompany(‘East Branch‘) comp.add(HRDepartment(‘East.Br HR‘)) comp.add(FinanceDepartment(‘East.Br Finance‘)) comp.add(RdDepartment("East.Br R&D")) root.add(comp) comp1 = ConcreteCompany(‘Northast Branch‘) comp1.add(HRDepartment(‘Northeast.Br HR‘)) comp1.add(FinanceDepartment(‘Northeast.Br Finance‘)) comp1.add(RdDepartment("Northeast.Br R&D")) comp.add(comp1) comp2 = ConcreteCompany(‘Southeast Branch‘) comp2.add(HRDepartment(‘Southeast.Br HR‘)) comp2.add(FinanceDepartment(‘Southeast.Br Finance‘)) comp2.add(RdDepartment("Southeast.Br R&D")) comp.add(comp2) root.display(1) root.listDuty()

列印結果:

-HeadQuarter
--HQ HR
--HQ Finance
--HQ R&D
--East Branch
---East.Br HR
---East.Br Finance
---East.Br R&D
---Northast Branch
----Northeast.Br HR
----Northeast.Br Finance
----Northeast.Br R&D
---Southeast Branch
----Southeast.Br HR
----Southeast.Br Finance
----Southeast.Br R&D
HQ HR Enrolling & Transfering management.
HQ FinanceFinance Management.
HQ R&DResearch & Development.
East.Br HR Enrolling & Transfering management.
East.Br FinanceFinance Management.
East.Br R&DResearch & Development.
Northeast.Br HR Enrolling & Transfering management.
Northeast.Br FinanceFinance Management.
Northeast.Br R&DResearch & Development.
Southeast.Br HR Enrolling & Transfering management.
Southeast.Br FinanceFinance Management.
Southeast.Br R&DResearch & Development.

 

優點:

1、節點增加和減少是非常自由和方便的,這也是樹形結構的一大特點;
2、所有節點,不管是分支節點還是葉子結點,不管是調用一個結點,還是調用一個結點群,都是非常方便的。

使用情境:

1、希望把對象表示分成部分-整體階層時

2、希望使用者忽略組合對象與單個對象的不同,使用者將統一的使用組合結構中的所有對象。

缺點

組合模式在定義樹葉和樹枝時直接使用了實作類別,不符合物件導向介面編程。與依賴倒置原則衝突。

python-組合模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.