我是靠谱客的博主 孤独芹菜,这篇文章主要介绍C# Facade外观模式中天河城购物出现的问题解决,现在分享给大家,希望可以做个参考。

这篇文章主要介绍了C#设计模式之Facade外观模式解决天河城购物问题,简单描述了外观模式的定义并结合具体实例分析了外观模式解决购物问题的相关步骤与操作技巧,需要的朋友可以参考下

本文实例讲述了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# Facade外观模式中天河城购物出现的问题解决的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是孤独芹菜最近收集整理的关于C# Facade外观模式中天河城购物出现的问题解决的全部内容,更多相关C#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部