C#設計模式-組合模式(Composite Pattern)

概念

組合是一種結構型設計模式, 你可以使用它將對象組合成樹狀結構, 並且能像使用獨立對象一樣使用它們。

組合模式(Composite Pattern)是將對象組合成樹形結構以表示‘部分-整體’的層次結構。組合模式使得用戶對單個對象和組合對象的使用具有一致性。

對於絕大多數需要生成樹狀結構的問題來說, 組合模式都是非常好的一種解決方案。 主要的功能是在整個樹狀結構上遞歸調用方法並對結果進行彙總。

結構圖

組合模式中的角色:

  • 抽象構件角色(Component):這是一個抽象角色,它給參加組合的對象定義出了公共的接口及默認行爲,可以用來管理所有的子對象(在透明式的組合模式是這樣的)。在安全式的組合模式裏,構件角色並不定義出管理子對象的方法,這一定義由樹枝結構對象給出。
  • 樹葉構件角色(Leaf):樹葉對象是沒有下級子對象的對象,定義出參加組合的原始對象的行爲。(原始對象的行爲可以理解爲沒有容器對象管理子對象的方法,或者原始對象行爲+管理子對象的行爲(Add,Remove等)=面對客戶代碼的接口行爲集合)
  • 樹枝構件角色(Composite):代表參加組合的有下級子對象的對象,樹枝對象給出所有管理子對象的方法實現,如Add、Remove等。組合模式實現的最關鍵的地方是--簡單對象和複合對象必須實現相同的接口。這就是組合模式能夠將組合對象和簡單對象進行一致處理的原因。

實現

組合模式實現的最關鍵的地方是——簡單對象和複合對象必須實現相同的接口。這就是組合模式能夠將組合對象和簡單對象進行一致處理的原因。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Composite
{
    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 branch = new Composite("branch");
            branch.add(new Leaf("branch BX"));
            branch.add(new Leaf("branch BY"));
            root.add(branch);

            // 創建第三層節點
            Composite branch2 = new Composite("branch2");
            branch2.add(new Leaf("branch2 BBX"));
            branch2.add(new Leaf("branch2 BBY"));
            root.add(branch2);

            // 葉子節點操作
            Composite branch3 = new Composite("branch3");
            Leaf leaf = new Leaf("Leaf L");
            Leaf leaf1 = new Leaf("Leaf L1");
            leaf.add(leaf1);
            leaf.delete(leaf1);
            branch3.add(leaf);
            branch3.add(leaf1);
            branch3.delete(leaf);
            root.add(branch3);

            // 顯示
            root.show(1);

            Console.Read();
        }
    }

    /// <summary>
    /// 抽象構件
    /// </summary>
    public abstract class Component
    {
        public string Name { get; set; }
        public Component(string name)
        {
            this.Name = name;
        }

        // 添加一個葉子構件或樹枝構件
        public abstract void add(Component component);
        // 刪除一個葉子構件或樹枝構件
        public abstract void delete(Component component);
        // 獲取分支下的所有葉子構件和樹枝構件
        public abstract void show(int depth);
    }

    /// <summary>
    /// 葉子構件
    /// </summary>
    public class Leaf : Component
    {
        public Leaf(string name):base(name)
        { }

        // 如果是葉子節點,則不允許進行添加節點,因爲葉子節點下再沒有節點了
        public override void add(Component component)
        {
            Console.WriteLine("葉子節點不能添加其他內容");
        }
        
        // 如果是葉子節點,則不允許進行刪除節點,因爲葉子節點下再沒有節點了
        public override void delete(Component component)
        {
            Console.WriteLine("葉子節點不能刪除內容");
        }

        public override void show(int depth)
        {
            // 輸出葉子節點
            for (int i = 0; i < depth; i++)
            {
                Console.Write("-");
            }
            Console.WriteLine(this.Name);
        }
    }

    /// <summary>
    /// 樹構件
    /// </summary>
    public class Composite : Component
    {
        protected List<Component> _children = new List<Component>();
        public Composite(string name) : base(name)
        { }

        public override void add(Component component)
        {
            _children.Add(component);
        }

        public override void delete(Component component)
        {
            _children.Remove(component);
        }

        public override void show(int depth)
        {
            // 輸出樹形結構層次
            for (int i=0; i<depth; i++)
            {
                Console.Write("-");
            }
            Console.WriteLine(this.Name);

            // 向下遍歷
            foreach (Component compontent in _children)
            {
                compontent.show(depth + 1);
            }
        }
    }
}

運行後結果:

葉子節點不能添加其他內容
葉子節點不能刪除內容
-root
--Leaf A
--Leaf B
--branch
---branch BX
---branch BY
--branch2
---branch2 BBX
---branch2 BBY
--branch3
---Leaf L1

使用場景

  • 需要表示一個對象整體或部分的層次結構。
  • 希望用戶忽略組合對象與單個對象的不同,用戶將統一地使用組合結構中的所有對象。

優缺點

優點:

  • 組合模式使得客戶端代碼可以一致地處理對象和對象容器,無需關係處理的單個對象,還是組合的對象容器。
  • 將”客戶代碼與複雜的對象容器結構“解耦。
  • 可以更容易地往組合對象中加入新的構件。

缺點:

  • 使得設計更加複雜。客戶端需要花更多時間理清類之間的層次關係。(這個是幾乎所有設計模式所面臨的問題)。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章