C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例
本文實例講述了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題。分享給大家供大家參考,具體如下:
一、理論定義
訪問者模式 提供了 一組 集合 對象 統(tǒng)一的 訪問接口,適合對 一個集合中的對象,進行邏輯操作,使 數據結構 和 邏輯結構分離。
二、應用舉例
需求描述:暑假來啦!三個小伙子組團,開車來 長隆歡樂世界玩。
每個人想玩的項目都不一樣,
旅游者 1 想玩:十環(huán)過山車,龍卷風暴,夢幻旋馬
旅游者 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.每個人要玩什么項目,都有一個標志
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>
/// 游玩的項目
/// </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) {
//每個旅游者想玩的項目不一樣。使用反射,方便調用
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)過山車,龍卷風暴,夢幻旋馬
/// </summary>
public class TouristOne : ITourist
{
/// <summary>
/// 十環(huán)過山車
/// </summary>
[PlayAttribute(PlayItem = "TenthRingRollerCoaster")]
public void Play_TenthRingRollerCoaster() {
Console.WriteLine("我是游客1,我現在玩的是:十環(huán)過山車");
}
/// <summary>
/// 龍卷風暴
/// </summary>
[PlayAttribute(PlayItem = "TornadoStorm")]
public void Play_TornadoStorm()
{
Console.WriteLine("我是游客1,我現在玩的是:龍卷風暴");
}
/// <summary>
/// 夢幻旋馬
/// </summary>
[PlayAttribute(PlayItem = "DreamHorse")]
public void Play_DreamHorse()
{
Console.WriteLine("我是游客1,我現在玩的是:夢幻旋馬");
}
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,我現在玩的是:空中警察");
}
/// <summary>
/// 歡樂摩天輪
/// </summary>
[PlayAttribute(PlayItem = "FerrisWheel")]
public void Play_FerrisWheel()
{
Console.WriteLine("我是游客2,我現在玩的是:歡樂摩天輪");
}
/// <summary>
/// 超級水戰(zhàn)
/// </summary>
[PlayAttribute(PlayItem = "SuperWater")]
public void Play_SuperWater()
{
Console.WriteLine("我是游客2,我現在玩的是:超級水戰(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,我現在玩的是:四維影院");
}
/// <summary>
/// 垂直極限
/// </summary>
[PlayAttribute(PlayItem = "VerticalLimit")]
public void Play_VerticalLimit()
{
Console.WriteLine("我是游客3,我現在玩的是:垂直極限");
}
/// <summary>
/// U型滑板
/// </summary>
[PlayAttribute(PlayItem = "UShapeSkateboard")]
public void Play_UShapeSkateboard()
{
Console.WriteLine("我是游客3,我現在玩的是:U型滑板");
}
public void Play(ChangLongHappyWorld happyWorld)
{
happyWorld.visit(this);
}
}
}
7.主函數
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)
{
//三個小伙子,開車到長隆歡樂世界 游玩, 每個人想玩的項目都不一樣。
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.運行結果

9.總結
運用C#的反射 來實現 復雜點的 訪問者模式 。
附:完整實例代碼點擊此處本站下載。
更多關于C#相關內容還可查看本站專題:《C#數據結構與算法教程》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數組操作技巧總結》及《C#面向對象程序設計入門教程》
希望本文所述對大家C#程序設計有所幫助。
相關文章
c# 9.0新特性nint和Pattern matching的使用方法
這篇文章主要介紹了c# 9.0新特性nint和Pattern matching的使用方法,文中講解非常細致,幫助你更好的學習c# 9.0,有需求的朋友可以參考下2020-06-06

