使用C# (.NET Core) 實現組合設計模式 (Composite Pattern)


本文的概念性內容來自深入淺出設計模式一書.

本文需結合上一篇文章(使用C# (.NET Core) 實現迭代器設計模式)一起看.

上一篇文章我們研究了多個菜單一起使用的問題.

需求變更

就當我們感覺我們的設計已經足夠好的時候, 新的需求來了, 我們不僅要支持多種菜單, 還要支持菜單下可以擁有子菜單.

例如我想在DinerMenu下添加一個甜點子菜單(dessert menu). 以我們目前的設計, 貌似無法實現該需求.

目前我們無法把dessertmenu放到MenuItem的數組里.

我們應該怎么做?

  • 我們需要一種類似樹形的結構, 讓其可以容納/適應菜單, 子菜單以及菜單項.
  • 我們還需要維護一種可以在該結構下遍歷所有菜單的方法, 要和使用遍歷器一樣簡單.
  • 遍歷條目的方法需要更靈活, 例如, 我可能只遍歷DinerMenu下的甜點菜單(dessert menu), 或者遍歷整個Diner Menu, 包括甜點菜單.

組合模式定義

組合模式允許你把對象們組合成樹形的結構, 從而來表示整體的層次. 通過組合, 客戶可以對單個對象或對象們的組合進行一致的處理.

先看一下樹形的結構, 擁有子元素的元素叫做節點(node), 沒有子元素的元素叫做葉子(leaf).

針對我們的需求:

菜單Menu就是節點, 菜單項MenuItem就是葉子.

 

針對需求我們可以創建出一種樹形結構, 它可以把嵌套的菜單或菜單項在相同的結構下進行處理.

組合和單個對象是指什么呢?

如果我們擁有一個樹形結構的菜單, 子菜單, 或者子菜單和菜單項一起, 那么就可以說任何一個菜單都是一個組合, 因為它可以包含其它菜單或菜單項.

而單獨的對象就是菜單項, 它們不包含其它對象.

使用組合模式, 我們可以把相同的操作作用於組合或者單個對象上. 也就是說, 大多數情況下我們可以忽略對象們的組合與單個對象之間的差別.

該模式的類圖:

客戶Client, 使用Component來操作組合中的對象.

Component定義了所有對象的接口, 包括組合節點與葉子. Component接口也可能實現了一些默認的操作, 這里就是add, remove, getChild.

葉子Leaf會繼承Component的默認操作, 但是有些操作也許並不適合葉子, 這個過會再說.

葉子Leaf沒有子節點.

組合Composite需要為擁有子節點的組件定義行為. 同樣還實現了葉子相關的操作, 其中有些操作可能不適合組合, 這種情況下異常可能會發生.

使用組合模式來設計菜單

 首先, 需要創建一個component接口, 它作為菜單和菜單項的共同接口, 這樣就可以在菜單或菜單項上調用同樣的方法了.

由於菜單和菜單項必須實現同一個接口, 但是畢竟它們的角色還是不同的, 所以並不是每一個接口里(抽象類里)的默認實現方法對它們都有意義. 針對毫無意義的默認方法, 有時最好的辦法是拋出一個運行時異常. 例如(NotSupportedException, C#).

MenuComponent:

using System;

namespace CompositePattern.Abstractions
{
    public abstract class MenuComponent
    {
        public virtual void Add(MenuComponent menuComponent)
        {
            throw new NotSupportedException();
        }

        public virtual void Remove(MenuComponent menuComponent)
        {
            throw new NotSupportedException();
        }

        public virtual MenuComponent GetChild(int i)
        {
            throw new NotSupportedException();
        }

        public virtual  string Name => throw new NotSupportedException();
        public virtual  string Description => throw new NotSupportedException();
        public virtual  double Price => throw new NotSupportedException();
        public virtual bool IsVegetarian => throw new NotSupportedException();

        public virtual void Print()
        {
            throw new NotSupportedException();
        }
    }
}

MenuItem:

using System;
using CompositePattern.Abstractions;

namespace CompositePattern.Menus
{
    public class MenuItem : MenuComponent
    {
        public MenuItem(string name, string description, double price, bool isVegetarian)
        {
            Name = name;
            Description = description;
            Price = price;
            IsVegetarian = isVegetarian;
        }

        public override string Name { get; }
        public override string Description { get; }
        public override double Price { get; }
        public override bool IsVegetarian { get; }

        public override void Print()
        {
            Console.Write($"\t{Name}");
            if (IsVegetarian)
            {
                Console.Write("(v)");
            }

            Console.WriteLine($", {Price}");
            Console.WriteLine($"\t\t -- {Description}");
        }
    }
}

Menu:

using System;
using System.Collections.Generic;
using CompositePattern.Abstractions;

namespace CompositePattern.Menus
{
    public class Menu : MenuComponent
    {
        readonly List<MenuComponent> _menuComponents;

        public Menu(string name, string description)
        {
            Name = name;
            Description = description;
            _menuComponents = new List<MenuComponent>();
        }

        public override string Name { get; }
        public override string Description { get; }

        public override void Add(MenuComponent menuComponent)
        {
            _menuComponents.Add(menuComponent);
        }

        public override void Remove(MenuComponent menuComponent)
        {
            _menuComponents.Remove(menuComponent);
        }

        public override MenuComponent GetChild(int i)
        {
            return _menuComponents[i];
        }

        public override void Print()
        {
            Console.Write($"\n{Name}");
            Console.WriteLine($", {Description}");
            Console.WriteLine("------------------------------");
        }
    }
}

注意Menu和MenuItem的Print()方法, 它們目前只能打印自己的東西, 還無法打印出整個組合. 也就是說如果打印的是菜單Menu的話, 那么它下面掛着的菜單Menu和菜單項MenuItems都應該被打印出來.

那么我們現在修復這個問題:

        public override void Print()
        {
            Console.Write($"\n{Name}");
            Console.WriteLine($", {Description}");
            Console.WriteLine("------------------------------");

            foreach (var menuComponent in _menuComponents)
            {
                menuComponent.Print();
            }
        }

服務員 Waitress:

using CompositePattern.Abstractions;

namespace CompositePattern.Waitresses
{
    public class Waitress
    {
        private readonly MenuComponent _allMenus;

        public Waitress(MenuComponent allMenus)
        {
            _allMenus = allMenus;
        }

        public void PrintMenu()
        {
            _allMenus.Print();
        }
    }
}

按照這個設計, 菜單組合在運行時將會是這個樣子:

下面我們來測試一下:

using System;
using CompositePattern.Menus;
using CompositePattern.Waitresses;

namespace CompositePattern
{
    class Program
    {
        static void Main(string[] args)
        {
            MenuTestDrive();
            Console.ReadKey();
        }

        static void MenuTestDrive()
        {
            var pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");
            var dinerMenu = new Menu("DINER MENU", "Lunch");
            var cafeMenu = new Menu("CAFE MENU", "Dinner");
            var dessertMenu = new Menu("DESSERT MENU", "Dessert of courrse!");

            var allMenus = new Menu("ALL MENUS", "All menus combined");
            allMenus.Add(pancakeHouseMenu);
            allMenus.Add(dinerMenu);
            allMenus.Add(cafeMenu);

            pancakeHouseMenu.Add(new MenuItem("Vegetarian BLT", "(Fakin’) Bacon with lettuce & tomato on whole wheat", true, 2.99));
            pancakeHouseMenu.Add(new MenuItem("K&B’s Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99));
            pancakeHouseMenu.Add(new MenuItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99));
            pancakeHouseMenu.Add(new MenuItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49));
            pancakeHouseMenu.Add(new MenuItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59));

            dinerMenu.Add(new MenuItem("Vegetarian BLT", "(Fakin’) Bacon with lettuce & tomato on whole wheat", true, 2.99));
            dinerMenu.Add(new MenuItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99));
            dinerMenu.Add(new MenuItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29));
            dinerMenu.Add(new MenuItem("Hotdog", "A hot dog, with saurkraut, relish, onions, topped with cheese", false, 3.05));
            dinerMenu.Add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89));

            dinerMenu.Add(dessertMenu);
            dessertMenu.Add(new MenuItem("Apple pie", "Apple pie with a flakey crust, topped with vanilla ice cream", true, 1.59));
            dessertMenu.Add(new MenuItem("Cheese pie", "Creamy New York cheessecake, with a chocolate graham crust", true, 1.99));
            dessertMenu.Add(new MenuItem("Sorbet", "A scoop of raspberry and a scoop of lime", true, 1.89));

            cafeMenu.Add(new MenuItem("Veggie Burger and Air Fries", "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", true, 3.99));
            cafeMenu.Add(new MenuItem("Soup of the day", "A cup of the soup of the day, with a side salad", false, 3.69));
            cafeMenu.Add(new MenuItem("Burrito", "A large burrito, with whole pinto beans, salsa, guacamole", true, 4.29));

            var waitress = new Waitress(allMenus);
            waitress.PrintMenu();

        }
    }
}

Ok.

慢着, 之前我們講過單一職責原則. 現在一個類擁有了兩個職責...

確實是這樣的, 我們可以這樣說, 組合模式用單一責任原則換取了透明性.

透明性是什么? 就是允許組件接口(Component interface)包括了子節點管理操作和葉子操作, 客戶可以一致的對待組合節點或葉子; 所以任何一個元素到底是組合節點還是葉子, 這件事對客戶來說是透明的. 

當然這么做會損失一些安全性. 客戶可以對某種類型的節點做出毫無意義的操作, 當然了, 這也是設計的決定.

組合迭代器

服務員現在想打印所有的菜單, 或者打印出所有的素食菜單項.

這里我們就需要實現組合迭代器.

要實現一個組合迭代器, 首先在抽象類MenuComponent里添加一個CreateEnumerator()的方法.

        public virtual IEnumerator<MenuComponent> CreateEnumerator()
        {
            return new NullEnumerator();
        }

注意NullEnumerator:

using System.Collections;
using System.Collections.Generic;
using CompositePattern.Abstractions;

namespace CompositePattern.Iterators
{
    public class NullEnumerator : IEnumerator<MenuComponent>
    {
        public bool MoveNext()
        {
            return false;
        }

        public void Reset()
        {
            
        }

        public MenuComponent Current => null;

        object IEnumerator.Current => Current;

        public void Dispose()
        {
        }
    }
}

我們可以用兩種方式來實現NullEnumerator:

  1. 返回null
  2. 當MoveNext()被調用的時候總返回false. (我采用的是這個)

這對MenuItem, 就沒有必要實現這個創建迭代器(遍歷器)方法了.

請仔細看下面這個組合迭代器(遍歷器)的代碼, 一定要弄明白, 這里面就是遞歸, 遞歸:

using System;
using System.Collections;
using System.Collections.Generic;
using CompositePattern.Abstractions;
using CompositePattern.Menus;

namespace CompositePattern.Iterators
{
    public class CompositeEnumerator : IEnumerator<MenuComponent>
    {
        private readonly Stack<IEnumerator<MenuComponent>> _stack = new Stack<IEnumerator<MenuComponent>>();

        public CompositeEnumerator(IEnumerator<MenuComponent> enumerator)
        {
            _stack.Push(enumerator);
        }

        public bool MoveNext()
        {
            if (_stack.Count == 0)
            {
                return false;
            }

            var enumerator = _stack.Peek();
            if (!enumerator.MoveNext())
            {
                _stack.Pop();
                return MoveNext();
            }

            return true;
        }

        public MenuComponent Current
        {
            get
            {
                var enumerator = _stack.Peek();
                var menuComponent = enumerator.Current;
                if (menuComponent is Menu)
                {
                    _stack.Push(menuComponent.CreateEnumerator());
                }
                return menuComponent;
            }
        }

        object IEnumerator.Current => Current;

        public void Reset()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
        }
    }
}

服務員 Waitress添加打印素食菜單的方法:

        public void PrintVegetarianMenu()
        {
            var enumerator = _allMenus.CreateEnumerator();
            Console.WriteLine("\nVEGETARIAN MENU\n--------");
            while (enumerator.MoveNext())
            {
                var menuComponent = enumerator.Current;
                try { if (menuComponent.IsVegetarian) { menuComponent.Print(); } } catch (NotSupportedException e) { }
            }
        }

注意這里的try catch, try catch一般是用來捕獲異常的. 我們也可以不這樣做, 我們可以先判斷它的類型是否為MenuItem, 但這個過程就讓我們失去了透明性, 也就是說 我們無法一致的對待Menu和MenuItem了.

我們也可以在Menu里面實現IsVegetarian屬性Get方法, 這可以保證透明性. 但是這樣做不一定合理, 也許其它人有更合理的原因會把Menu的IsVegetarian給實現了. 所以我們還是使用try catch吧.

 

測試:

Ok.

總結

設計原則: 一個類只能有一個讓它改變的原因.

迭代器模式: 迭代器模式提供了一種訪問聚合對象(例如集合)元素的方式, 而且又不暴露該對象的內部表示.

組合模式: 組合模式允許你把對象們組合成樹形的結構, 從而來表示整體的層次. 通過組合, 客戶可以對單個對象或對象們的組合進行一致的處理.

 

針對C#來說, 上面的代碼肯定不是最簡單最直接的實現方式, 但是通過這些比較原始的代碼可以對設計模式理解的更好一些.

改系列的源碼在: https://github.com/solenovex/Head-First-Design-Patterns-in-CSharp

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM