C#設(shè)計(jì)模式之Visitor訪問者模式解決長隆歡樂世界問題實(shí)例
本文實(shí)例講述了C#設(shè)計(jì)模式之Visitor訪問者模式解決長隆歡樂世界問題。分享給大家供大家參考,具體如下:
一、理論定義
訪問者模式 提供了 一組 集合 對象 統(tǒng)一的 訪問接口,適合對 一個集合中的對象,進(jìn)行邏輯操作,使 數(shù)據(jù)結(jié)構(gòu) 和 邏輯結(jié)構(gòu)分離。
二、應(yīng)用舉例
需求描述:暑假來啦!三個小伙子組團(tuán),開車來 長隆歡樂世界玩。
每個人想玩的項(xiàng)目都不一樣,
旅游者 1 想玩:十環(huán)過山車,龍卷風(fēng)暴,夢幻旋馬
旅游者 2 想玩:空中警察,歡樂摩天輪,超級水戰(zhàn)
旅游者 3 想玩:四維影院,垂直極限,U型滑板
車開到長隆后,就開始各自Enjoy啦!?。?/p>
三、具體編碼
1.一個旅游者接口,里面有一個Play游玩 方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Visitor { public interface ITourist { /// <summary> /// 游玩 /// </summary> /// <param name="happyWorld">長隆歡樂世界</param> void Play(ChangLongHappyWorld happyWorld); } }
2.每個人要玩什么項(xiàng)目,都有一個標(biāo)志
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Visitor { [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class PlayAttribute : Attribute { private string _PlayItem; /// <summary> /// 游玩的項(xiàng)目 /// </summary> public string PlayItem { get { return _PlayItem; } set { _PlayItem = value; } } } }
3.長隆歡樂世界
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Com.Design.Gof.Visitor { /// <summary> /// 長隆歡樂世界 /// </summary> public class ChangLongHappyWorld { /// <summary> /// 接待各個訪問者 /// </summary> /// <param name="visitor"></param> public void visit(ITourist visitor) { //每個旅游者想玩的項(xiàng)目不一樣。使用反射,方便調(diào)用 MethodInfo[] method = visitor.GetType().GetMethods(); foreach (MethodInfo m in method) { object[] property= m.GetCustomAttributes(false); string customerAttribute = null; if (property.Length>0) { customerAttribute = property[0].ToString(); } if (customerAttribute == "Com.Design.Gof.Visitor.PlayAttribute") { m.Invoke(visitor, new object[] { }); } } } } }
4.旅游者 1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Visitor { /// <summary> /// 旅游者 1 想玩:十環(huán)過山車,龍卷風(fēng)暴,夢幻旋馬 /// </summary> public class TouristOne : ITourist { /// <summary> /// 十環(huán)過山車 /// </summary> [PlayAttribute(PlayItem = "TenthRingRollerCoaster")] public void Play_TenthRingRollerCoaster() { Console.WriteLine("我是游客1,我現(xiàn)在玩的是:十環(huán)過山車"); } /// <summary> /// 龍卷風(fēng)暴 /// </summary> [PlayAttribute(PlayItem = "TornadoStorm")] public void Play_TornadoStorm() { Console.WriteLine("我是游客1,我現(xiàn)在玩的是:龍卷風(fēng)暴"); } /// <summary> /// 夢幻旋馬 /// </summary> [PlayAttribute(PlayItem = "DreamHorse")] public void Play_DreamHorse() { Console.WriteLine("我是游客1,我現(xiàn)在玩的是:夢幻旋馬"); } public void Play(ChangLongHappyWorld happyWorld) { happyWorld.visit(this); } } }
5.旅游者 2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Visitor { /// <summary> /// 旅游者 2 想玩:空中警察,歡樂摩天輪,超級水戰(zhàn) /// </summary> public class TouristTwo : ITourist { /// <summary> /// 空中警察 /// </summary> [PlayAttribute(PlayItem = "AirPolice")] public void Play_AirPolice() { Console.WriteLine("我是游客2,我現(xiàn)在玩的是:空中警察"); } /// <summary> /// 歡樂摩天輪 /// </summary> [PlayAttribute(PlayItem = "FerrisWheel")] public void Play_FerrisWheel() { Console.WriteLine("我是游客2,我現(xiàn)在玩的是:歡樂摩天輪"); } /// <summary> /// 超級水戰(zhàn) /// </summary> [PlayAttribute(PlayItem = "SuperWater")] public void Play_SuperWater() { Console.WriteLine("我是游客2,我現(xiàn)在玩的是:超級水戰(zhàn)"); } public void Play(ChangLongHappyWorld happyWorld) { happyWorld.visit(this); } } }
6.旅游者 3
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Visitor { /// <summary> /// 旅游者 3 想玩:四維影院,垂直極限,U型滑板 /// </summary> public class TouristThree : ITourist { /// <summary> /// 四維影院 /// </summary> [PlayAttribute(PlayItem = "AirPolice")] public void Play_Cinema4D() { Console.WriteLine("我是游客3,我現(xiàn)在玩的是:四維影院"); } /// <summary> /// 垂直極限 /// </summary> [PlayAttribute(PlayItem = "VerticalLimit")] public void Play_VerticalLimit() { Console.WriteLine("我是游客3,我現(xiàn)在玩的是:垂直極限"); } /// <summary> /// U型滑板 /// </summary> [PlayAttribute(PlayItem = "UShapeSkateboard")] public void Play_UShapeSkateboard() { Console.WriteLine("我是游客3,我現(xiàn)在玩的是:U型滑板"); } public void Play(ChangLongHappyWorld happyWorld) { happyWorld.visit(this); } } }
7.主函數(shù)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Com.Design.Gof.Visitor; namespace Com.Design.Gof.Test { class Program { static void Main(string[] args) { //三個小伙子,開車到長隆歡樂世界 游玩, 每個人想玩的項(xiàng)目都不一樣。 List<ITourist> list = new List<ITourist> { new TouristOne(), new TouristTwo(), new TouristThree() }; //車開到了長隆 南大門,長隆到了 ChangLongHappyWorld happyWorld = new ChangLongHappyWorld(); //開始 游玩 長隆啦!! foreach (var visit in list) { visit.Play(happyWorld); Console.WriteLine("------------------------------------------------"); } Console.ReadKey(); } } }
8.運(yùn)行結(jié)果
9.總結(jié)
運(yùn)用C#的反射 來實(shí)現(xiàn) 復(fù)雜點(diǎn)的 訪問者模式 。
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
利用C#編寫Linux守護(hù)進(jìn)程實(shí)例代碼
如今的編程是一場程序員和上帝的競賽,程序員要開發(fā)出更大更好、傻瓜都會用到軟件,下面這篇文章主要給大家介紹了關(guān)于利用C#編寫Linux守護(hù)進(jìn)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01C#基于Socket的TCP通信實(shí)現(xiàn)聊天室案例
這篇文章主要為大家詳細(xì)介紹了C#基于Socket的TCP通信實(shí)現(xiàn)聊天室案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C#利用時(shí)間和隨即字符串創(chuàng)建唯一的訂單編號
本文介紹了利用時(shí)間和隨機(jī)字符串組合生成唯一訂單號的示例,從而保證訂單號不會重復(fù),希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-03-03C#簡單嵌套flash讀取數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#簡單嵌套flash讀取數(shù)據(jù)的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11C#實(shí)現(xiàn)ComboBox變色的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)ComboBox變色的效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01解析c#操作excel后關(guān)閉excel.exe的方法
C#和Asp.net下excel進(jìn)程一被打開,有時(shí)就無法關(guān)閉,尤其是website.對關(guān)閉該進(jìn)程有過GC、release等方法,但這些方法并不是在所有情況下均適用2013-07-07最新評論