C# Facade外观模式中天河城购物出现的问题解决

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

本文实例讲述了C#设计模式之Facade外观模式解决天河城购物问题。分享给大家供大家参考,具体如下:

一、理论定义

外观模式   把  分散的子系统,集合成一个系统,提供一站式服务。

二、应用举例

需求描述: 聂小倩 和 宁采臣是一对小富则安 的聊斋夫妻。住在比较偏远的小乡村。
今天,两人初次来到大城市广州,听说天河城提供一站式服务,不像小城市那样,买个东西  得  东奔西跑。
在一个地方,就可以买到 自己想要的衣服,电脑,鞋子,Iphone,还可以看大片,
吃冰淇淋,吃真功夫,买化妆品,珠宝首饰。天河城,果然是一宝地啊。
Ok,边走边看。

三、具体编码

1.阿迪达斯

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Com.Design.Gof.Facade{  ///   /// 阿迪达斯  ///   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{  ///   /// 飞扬影城  ///   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{  ///   /// 国美电器  ///   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{  ///   /// 哈根达斯  ///   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{  ///   /// 真功夫  ///   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{  ///   /// 六福珠宝  ///   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{  ///   /// 耐克  ///   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{  ///   /// ONLY时装  ///   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{  ///   /// 苏宁电器  ///   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{  ///   /// Veromoda国际时装品牌  ///   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{  ///   /// 消费店子  ///   public enum ShopOption  {    Adidas = 1, DKNY = 2, GoMe = 3,    NIKE = 4, Suning = 5, Veromoda = 6,    FeiYangMovie = 7, HaagenDaz = 8, LukFook = 9, KungFu = 10  }  ///   /// 消费单  ///   public class Bill {    ///     /// 要去的消费店    ///     public ShopOption Item { get; set; }    ///     /// 去这个店要买啥    ///     public string Something { get; set; }  }  public class Consumer  {    ///     /// 消费单    ///     public IList Items { get; set; }    ///     /// 姓名    ///     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{  ///   /// 天河城  ///   public class TeeMall  {    private static readonly Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"Com.Design.Gof.dll");    ///     /// 一站式服务    ///     ///     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 {         new Bill{ Item=ShopOption.Adidas, Something="运动服"},         new Bill{ Item=ShopOption.GoMe, Something="苹果IPhone智能手机"},         new Bill{ Item=ShopOption.FeiYangMovie, Something=""},         new Bill{ Item=ShopOption.KungFu, Something="香菇炖鸡"},          new Bill{ Item=ShopOption.LukFook, Something="金项链"},        }      };      TeeMall.OfferService(consumer);      //消费者 2      consumer = new Consumer      {        Name = "宁采臣",        //消费单        Items = new List {         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.运行结果

C# Facade外观模式中天河城购物出现的问题解决

15.总结

天河城 TeeMall 理论上应该包括 所有 商场的引用,

这里用反射 避免了这一动作。

以上就是C# Facade外观模式中天河城购物出现的问题解决的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2493606.html

(0)
上一篇 2025年3月5日 01:34:44
下一篇 2025年3月5日 01:34:57

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • C#中String类型与json之间相互转换的实现方法

    这篇文章主要介绍了c#实现string类型和json之间的相互转换功能,涉及c# json格式数据的构造、转换相关操作技巧,需要的朋友可以参考下 本文实例讲述了C#实现String类型和json之间的相互转换功能。分享给大家供大家参考,具体…

    编程技术 2025年3月5日
    100
  • C#中Builder生成器模式解决配置电脑的问题详解

    这篇文章主要介绍了c#设计模式之builder生成器模式解决带老婆配置电脑问题,简单介绍了生成器模式的概念、功能并结合具体实例形式分析了c#生成器模式解决配电脑问题的步骤与相关操作技巧,需要的朋友可以参考下 本文实例讲述了C#设计模式之Bu…

    2025年3月5日
    200
  • C#实现Json序列化删除null值的方法实例

    要将一个对象序列化,可是如果对象的属性为null的时候,我们想将属性为null的都去掉,怎么处理呢?其实方法很简单的,下面就跟随脚本之家小编一起学习c#中 json 序列化去掉null值的方法吧 要将一个对象序列化,可是如果对象的属性为nu…

    编程技术 2025年3月5日
    200
  • C#中发送邮件的实现方法详解

    这篇文章主要为大家详细介绍了c#实现发送邮件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 #region 发送邮件部分    private …

    编程技术 2025年3月5日
    200
  • C#中关于Minutes与TotalMinutes的区别

    今天测试提了一个bug,说是消息提醒的时机不对,设置的提前2小时,还没到就提醒了。 看了下代码 (m.ExpectReceiveTime – DateTime.Now).Minutes 执行(m.ExpectReceiveTime – Da…

    编程技术 2025年3月5日
    200
  • C#中关于表达式树的简单介绍

    表达式树可以说是linq的核心之一,为什么是linq的核心之一呢?因为表达式树使得c#不再是仅仅能编译成il,我们可以通过c#生成一个表达式树,将结果作为一个中间格式,在将其转换成目标平台上的本机语言。比如sql。我们常用的linq to …

    2025年3月5日
    200
  • C# WinForm跨线程访问控件的图文详解

     问题出现:  在WinForm 处理多线程访问主线程的控件时候,就会出现如图所示的错误对话框:          解决方案:      方案一:去掉线程访问主线程UI控件的安全检查,使用:  Control.CheckForIllegal…

    2025年3月5日 编程技术
    200
  • C#中VB.NET给Word文档添加/撤销书签的实例

    在现代办公环境中,阅读或者编辑较长篇幅的word文档时,想要在文档中某一处或者几处留下标记,方便日后查找、修改时,需要在相对应的文档位置插入书签。那对于开发者而言,在c#或者vb.net语言环境中,如何来快速、简便的插入书签呢,我分享一下我…

    2025年3月5日
    200
  • C#中异步编程4async与await异步程序开发的实例分析

    随着c#异步程序开发系列的深入,你会发现编写异步程序越发简单。事物的发展就是这样的规律,从简单到复杂再到简单。 在C# 5.0中我们可以通过async与await关键字实现快捷的异步程序开发,如下:         static void …

    2025年3月5日
    200
  • .Net实现微信JS-SDK分享功能代码展示

    这篇文章主要介绍了微信js-sdk分享功能的.net实现代码的相关资料,需要的朋友可以参考下 JS-SDK接口是什么? 为了方便开发者实现微信内的网页(基于微信浏览器访问的网页)功能,比如拍照、选图、语音、位置等手机系统的能力,并方便开发者…

    2025年3月5日
    200

发表回复

登录后才能评论