本文實例講述了c#設計模式之visitor訪問者模式解決長隆歡樂世界問題。分享給大家供大家參考,具體如下:
一、理論定義
訪問者模式 提供了 一組 集合 對象 統一的 訪問接口,適合對 一個集合中的對象,進行邏輯操作,使 數據結構 和 邏輯結構分離。
二、應用舉例
需求描述:暑假來啦!三個小伙子組團,開車來 長隆歡樂世界玩。
每個人想玩的項目都不一樣,
旅游者 1 想玩:十環過山車,龍卷風暴,夢幻旋馬
旅游者 2 想玩:空中警察,歡樂摩天輪,超級水戰
旅游者 3 想玩:四維影院,垂直極限,u型滑板
車開到長隆后,就開始各自enjoy啦!!!
三、具體編碼
1.一個旅游者接口,里面有一個play游玩 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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.每個人要玩什么項目,都有一個標志
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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.長隆歡樂世界
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.visitor { /// <summary> /// 旅游者 1 想玩:十環過山車,龍卷風暴,夢幻旋馬 /// </summary> public class touristone : itourist { /// <summary> /// 十環過山車 /// </summary> [playattribute(playitem = "tenthringrollercoaster" )] public void play_tenthringrollercoaster() { console.writeline( "我是游客1,我現在玩的是:十環過山車" ); } /// <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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.visitor { /// <summary> /// 旅游者 2 想玩:空中警察,歡樂摩天輪,超級水戰 /// </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> /// 超級水戰 /// </summary> [playattribute(playitem = "superwater" )] public void play_superwater() { console.writeline( "我是游客2,我現在玩的是:超級水戰" ); } public void play(changlonghappyworld happyworld) { happyworld.visit( this ); } } } |
6.旅游者 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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.主函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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#程序設計有所幫助。
原文鏈接:http://www.cnblogs.com/HCCZX/archive/2012/08/02/2619723.html