本文实例讲述了C#设计模式之Facade外观模式解决天河城购物问题。分享给大家供大家参考,具体如下:
一、理论定义
外观模式 把 分散的子系统,集合成一个系统,提供一站式服务。
二、应用举例
需求描述: 聂小倩 和 宁采臣是一对小富则安 的聊斋夫妻。住在比较偏远的小乡村。
今天,两人初次来到大城市广州,听说天河城提供一站式服务,不像小城市那样,买个东西 得 东奔西跑。
在一个地方,就可以买到 自己想要的衣服,电脑,鞋子,Iphone,还可以看大片,
吃冰淇淋,吃真功夫,买化妆品,珠宝首饰。天河城,果然是一宝地啊。
Ok,边走边看。
三、具体编码
1.阿迪达斯
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.飞扬影城
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.国美电器
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.哈根达斯
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.真功夫
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.六福珠宝
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.耐克
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
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.苏宁电器
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国际时装品牌
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.消费者
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.天河城---一站式服务
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.主函数调用
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外观模式中天河城购物出现的问题解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)