本文實例講述了c#設計模式之facade外觀模式解決天河城購物問題。分享給大家供大家參考,具體如下:
一、理論定義
外觀模式 把 分散的子系統,集合成一個系統,提供一站式服務。
二、應用舉例
需求描述: 聶小倩 和 寧采臣是一對小富則安 的聊齋夫妻。住在比較偏遠的小鄉村。
今天,兩人初次來到大城市廣州,聽說天河城提供一站式服務,不像小城市那樣,買個東西 得 東奔西跑。
在一個地方,就可以買到 自己想要的衣服,電腦,鞋子,iphone,還可以看大片,
吃冰淇淋,吃真功夫,買化妝品,珠寶首飾。天河城,果然是一寶地啊。
ok,邊走邊看。
三、具體編碼
1.阿迪達斯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 阿迪達斯 /// </summary> public class adidas { public void serivce( string something) { console.writeline( "在阿迪達斯購買了: " +something); } } } |
2.飛揚影城
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 飛揚影城 /// </summary> public class feiyangmovie { public void serivce( string something) { console.writeline( "在飛揚影城看了一部電影: " + something); } } } |
3.國美電器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 國美電器 /// </summary> public class gome { public void serivce( string something) { console.writeline( "在國美電器 買了: " + something); } } } |
4.哈根達斯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 哈根達斯 /// </summary> public class haagendaz { public void serivce( string something) { console.writeline( "在哈根達斯 買了: " + something); } } } |
5.真功夫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 真功夫 /// </summary> public class kungfu { public void serivce( string something) { console.writeline( "在真功夫 吃了: " + something); } } } |
6.六福珠寶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 六福珠寶 /// </summary> public class lukfook { public void serivce( string something) { console.writeline( "在六福珠寶 買了: " + something); } } } |
7.耐克
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 耐克 /// </summary> public class nike { public void serivce( string something) { console.writeline( "在耐克店 買了: " + something); } } } |
8.only
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// only時裝 /// </summary> public class only { public void serivce( string something) { console.writeline( "在only時裝 買了: " + something); } } } |
9.蘇寧電器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// 蘇寧電器 /// </summary> public class suning { public void serivce( string something) { console.writeline( "在蘇寧電器 買了: " + something); } } } |
10.veromoda國際時裝品牌
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using system; using system.collections.generic; using system.linq; using system.text; namespace com.design.gof.facade { /// <summary> /// veromoda國際時裝品牌 /// </summary> public class veromoda { public void serivce( string something) { console.writeline(something); } } } |
11.消費者
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.facade { /// <summary> /// 消費店子 /// </summary> public enum shopoption { adidas = 1, dkny = 2, gome = 3, nike = 4, suning = 5, veromoda = 6, feiyangmovie = 7, haagendaz = 8, lukfook = 9, kungfu = 10 } /// <summary> /// 消費單 /// </summary> public class bill { /// <summary> /// 要去的消費店 /// </summary> public shopoption item { get ; set ; } /// <summary> /// 去這個店要買啥 /// </summary> public string something { get ; set ; } } public class consumer { /// <summary> /// 消費單 /// </summary> public ilist<bill> items { get ; set ; } /// <summary> /// 姓名 /// </summary> public string name { get ; set ; } } } |
12.天河城---一站式服務
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
|
using system; using system.collections.generic; using system.linq; using system.text; using system.reflection; namespace com.design.gof.facade { /// <summary> /// 天河城 /// </summary> public class teemall { private static readonly assembly assembly = assembly.loadfile(appdomain.currentdomain.basedirectory + @"\com.design.gof.dll" ); /// <summary> /// 一站式服務 /// </summary> /// <param name="consumer"></param> public void offerservice(consumer consumer) { console.writeline( "我是: " + consumer.name+ ",不差錢,今天來天河城玩: " ); console.writeline( "----------------------------------------------" ); foreach (bill item in consumer.items) { object obj= assembly.createinstance( "com.design.gof.facade." + item.item); methodinfo info = obj.gettype().getmethod( "serivce" ); info.invoke(obj, new object [] { item.something }); } console.writeline(); } } } |
13.主函數調用
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
41
42
43
44
|
using system; using system.collections.generic; using system.linq; using system.text; using com.design.gof.facade; namespace com.design.gof.test { class program { static void main( string [] args) { //天河城購物中心 teemall teemall = new teemall(); //消費者 1 consumer consumer = new consumer { name= "聶小倩" , //消費單 items = new list<bill> { new bill{ item=shopoption.adidas, something= "運動服" }, new bill{ item=shopoption.gome, something= "蘋果iphone智能手機" }, new bill{ item=shopoption.feiyangmovie, something= "<冰河世紀 4>" }, new bill{ item=shopoption.kungfu, something= "香菇燉雞" }, new bill{ item=shopoption.lukfook, something= "金項鏈" }, } }; teemall.offerservice(consumer); //消費者 2 consumer = new consumer { name = "寧采臣" , //消費單 items = new list<bill> { new bill{ item=shopoption.feiyangmovie, something= "《太空一號》" }, new bill{ item=shopoption.veromoda, something= "然后去了veromoda時裝,買了一套服裝" }, new bill{ item=shopoption.haagendaz, something= "買了一雪糕" }, new bill{ item=shopoption.suning, something= "在蘇寧看買平板電腦" }, } }; teemall.offerservice(consumer); console.readkey(); } } } |
14.運行結果
15.總結
天河城 teemall 理論上應該包括 所有 商場的引用,
這里用反射 避免了這一動作。
附:完整實例代碼點擊此處本站下載。
希望本文所述對大家c#程序設計有所幫助。
原文鏈接:http://www.cnblogs.com/HCCZX/archive/2012/08/13/2636459.html