【設計模式】分公司是一個部門? —- 組合模式

來源:互聯網
上載者:User

一,概述

        概念:將對象組合成樹形結構以表示“部分整體”的階層。組合模式使得使用者對單個對象和使用具有一致性。


                                                      

        Client:用戶端

        Component:組合模式中對象介面聲明,在適當情況下,實現所有類共有介面的預設行為。聲明一個介面用於訪問和管理其子組件。

        SubMenu:表示節點對象,但是沒有子節點。

        Composite:定義有枝節點的行為,用來儲存子組件,實現Component介面中與子組件有關的操作,比如增加和刪除。


二,基本代碼(C#)

         由於C++中STL ---  list 對於抽象對象的識別和支援跟C##不同,沒有寫成C++的形式。

         不能由基類聲明的抽象方法,調用子類的具體實現!!!


using System;using System.Collections.Generic;using System.Text;namespace 組合模式{    class Program    {        static void Main(string[] args)        {            Composite root = new Composite("root");            root.Add(new Leaf("Leaf A"));            root.Add(new Leaf("Leaf B"));            Composite comp = new Composite("Composite X");            comp.Add(new Leaf("Leaf XA"));            comp.Add(new Leaf("Leaf XB"));            root.Add(comp);            Composite comp2 = new Composite("Composite XY");            comp2.Add(new Leaf("Leaf XYA"));            comp2.Add(new Leaf("Leaf XYB"));            comp.Add(comp2);            root.Add(new Leaf("Leaf C"));            Leaf leaf = new Leaf("Leaf D");            root.Add(leaf);            root.Remove(leaf);            root.Display(1);            Console.Read();        }    }    abstract class Component    {        protected string name;        public Component(string name)        {            this.name = name;        }        public abstract void Add(Component c);        public abstract void Remove(Component c);        public abstract void Display(int depth);    }    class Composite : Component    {        private List<Component> children = new List<Component>();        public Composite(string name)            : base(name)        { }        public override void Add(Component c)        {            children.Add(c);        }        public override void Remove(Component c)        {            children.Remove(c);        }        public override void Display(int depth)        {            Console.WriteLine(new String('-', depth) + name);            foreach (Component component in children)            {                component.Display(depth + 2);            }        }    }    class Leaf : Component    {        public Leaf(string name)            : base(name)        { }        public override void Add(Component c)        {            Console.WriteLine("Cannot add to a leaf");        }        public override void Remove(Component c)        {            Console.WriteLine("Cannot remove from a leaf");        }        public override void Display(int depth)        {            Console.WriteLine(new String('-', depth) + name);        }    }}

三,公司管理系統 採用組合模式

using System;using System.Collections.Generic;using System.Text;namespace 組合模式{    class Program    {        static void Main(string[] args)        {            ConcreteCompany root = new ConcreteCompany("北京總公司");            root.Add(new HRDepartment("總公司人力資源部"));            root.Add(new FinanceDepartment("總公司財務部"));            ConcreteCompany comp = new ConcreteCompany("上海華東分公司");            comp.Add(new HRDepartment("華東分公司人力資源部"));            comp.Add(new FinanceDepartment("華東分公司財務部"));            root.Add(comp);            ConcreteCompany comp1 = new ConcreteCompany("南京辦事處");            comp1.Add(new HRDepartment("南京辦事處人力資源部"));            comp1.Add(new FinanceDepartment("南京辦事處財務部"));            comp.Add(comp1);            ConcreteCompany comp2 = new ConcreteCompany("杭州辦事處");            comp2.Add(new HRDepartment("杭州辦事處人力資源部"));            comp2.Add(new FinanceDepartment("杭州辦事處財務部"));            comp.Add(comp2);            Console.WriteLine("\n結構圖:");            root.Display(1);            Console.WriteLine("\n職責:");            root.LineOfDuty();            Console.Read();        }    }    abstract class Company    {        protected string name;        public Company(string name)        {            this.name = name;        }        public abstract void Add(Company c);//增加        public abstract void Remove(Company c);//移除        public abstract void Display(int depth);//顯示        public abstract void LineOfDuty();//履行職責    }    class ConcreteCompany : Company    {        private List<Company> children = new List<Company>();        public ConcreteCompany(string name)            : base(name)        { }        public override void Add(Company c)        {            children.Add(c);        }        public override void Remove(Company c)        {            children.Remove(c);        }        public override void Display(int depth)        {            Console.WriteLine(new String('-', depth) + name);            foreach (Company component in children)            {                component.Display(depth + 2);            }        }        //履行職責        public override void LineOfDuty()        {            foreach (Company component in children)            {                component.LineOfDuty();            }        }    }    //人力資源部    class HRDepartment : Company    {        public HRDepartment(string name)            : base(name)        { }        public override void Add(Company c)        {        }        public override void Remove(Company c)        {        }        public override void Display(int depth)        {            Console.WriteLine(new String('-', depth) + name);        }        public override void LineOfDuty()        {            Console.WriteLine("{0} 員工招聘培訓管理", name);        }    }    //財務部    class FinanceDepartment : Company    {        public FinanceDepartment(string name)            : base(name)        { }        public override void Add(Company c)        {        }        public override void Remove(Company c)        {        }        public override void Display(int depth)        {            Console.WriteLine(new String('-', depth) + name);        }        public override void LineOfDuty()        {            Console.WriteLine("{0} 公司財務收支管理", name);        }    }}


       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.