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

来源:https://www.cnblogs.com/cgzl/archive/2018/04/22/8907753.html
-Advertisement-
Play Games

本文的概念性內容來自深入淺出設計模式一書. 本文需結合上一篇文章(使用C# (.NET Core) 實現迭代器設計模式)一起看. 上一篇文章我們研究了多個菜單一起使用的問題. 需求變更 就當我們感覺我們的設計已經足夠好的時候, 新的需求來了, 我們不僅要支持多種菜單, 還要支持菜單下可以擁有子菜單. ...


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

本文需結合上一篇文章(使用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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 上一篇我們已經初步的構建起了一個學生管理系統的模型,現在接著來繼續完善它吧。 1、上傳圖片/文件等資源 有時候需要添加一些附件,例如,新生剛入學,大家相互之間還不熟悉,希望能通過照片來加深印象,並且方便教學管理。 首先,對demo/urls.py文件進行改造,給urlpatterns添加static ...
  • 一.http協議 二.hibernate緩存模式,級別;Hibernate和mybatis的區別和優缺點 三.SQL優化經驗 四.分散式集群和Redis 五.Spring Aop,動態代理; 六.多線程安全問題,多線程實現 thread local 一.HTTP協議(HyperText Transf ...
  • 最近在客戶項目上剛好遇到一個問題,項目需求是要獲取某台機床的實時狀態,問題點剛好就在於該機床不是傳統意義上的數控機床,也不是PLC控制器,只有一個上傳下載程式文件的應用程式,上面剛好有幾個按鈕可以大概判斷當前工作狀態,轉眼一想,是否可以實時獲取幾個按鈕的狀態,從而簡單分析下就確定機床加工狀態。 說乾 ...
  • 5-1 條件測試 :編寫一系列條件測試;將每個測試以及你對其結果的預測和實際結果都列印出來。你編寫的代碼應類似於下麵這樣: car = 'subaru' print("Is car == 'subaru'? I predict True.") print(car == 'subaru') print ...
  • Python中的random模塊用於生成隨機數。 下麵具體介紹random模塊的功能: 1.random.random() #用於生成一個0到1的 隨機浮點數:0<= n < 1.0 2.random.uniform(a,b) #用於生成一個指定範圍內的隨機符點數,兩個參數其中一個是上限,一個是下限 ...
  • using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Reflection; using Syst ...
  • 主題 本篇我將會介紹驗證用戶的機制當賬戶被創建的時候,同樣 這個過程主要有IUserValidator這個介面來實現的,和密碼驗證一樣Identity同樣也內置已經實現好的賬戶驗證。賬戶驗證的比較簡單,我會先介紹內置的IUserValidator的使用,然後會簡單介紹一些源代碼,最後會演示怎實現一個 ...
  • 問題背景: 最近做一個非常簡單的功能,就是使用ajax請求的方式從服務端請求一段下拉表的數據。 以前也有做過這個功能,只不過這次做這個功能的時候冒出了一個想法: 我請求的這段數據它是一段相對比較固定的數據,也就是說它不怎麼改變,也許幾個月才會改變一次。由於這種數據的變化周期很長,所以以前做這種功能的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...