服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#设计模式之Facade外观模式解决天河城购物问题示例

C#设计模式之Facade外观模式解决天河城购物问题示例

2022-01-21 14:14GhostRider C#

这篇文章主要介绍了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.运行结果

C#设计模式之Facade外观模式解决天河城购物问题示例

15.总结

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

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

附:完整实例代码点击此处本站下载

希望本文所述对大家c#程序设计有所帮助。

原文链接:http://www.cnblogs.com/HCCZX/archive/2012/08/13/2636459.html

延伸 · 阅读

精彩推荐
  • C#C#通过KD树进行距离最近点的查找

    C#通过KD树进行距离最近点的查找

    这篇文章主要为大家详细介绍了C#通过KD树进行距离最近点的查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    帆帆帆6112022-01-22
  • C#深入解析C#中的交错数组与隐式类型的数组

    深入解析C#中的交错数组与隐式类型的数组

    这篇文章主要介绍了深入解析C#中的交错数组与隐式类型的数组,隐式类型的数组通常与匿名类型以及对象初始值设定项和集合初始值设定项一起使用,需要的...

    C#教程网6172021-11-09
  • C#Unity3D实现虚拟按钮控制人物移动效果

    Unity3D实现虚拟按钮控制人物移动效果

    这篇文章主要为大家详细介绍了Unity3D实现虚拟按钮控制人物移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    shenqingyu060520232410972022-03-11
  • C#C#实现XML文件读取

    C#实现XML文件读取

    这篇文章主要为大家详细介绍了C#实现XML文件读取的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Just_for_Myself6702022-02-22
  • C#C# 实现对PPT文档加密、解密及重置密码的操作方法

    C# 实现对PPT文档加密、解密及重置密码的操作方法

    这篇文章主要介绍了C# 实现对PPT文档加密、解密及重置密码的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    E-iceblue5012022-02-12
  • C#C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    这篇文章主要介绍了C#设计模式之Visitor访问者模式解决长隆欢乐世界问题,简单描述了访问者模式的定义并结合具体实例形式分析了C#使用访问者模式解决长...

    GhostRider9502022-01-21
  • C#C#裁剪,缩放,清晰度,水印处理操作示例

    C#裁剪,缩放,清晰度,水印处理操作示例

    这篇文章主要为大家详细介绍了C#裁剪,缩放,清晰度,水印处理操作示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    吴 剑8332021-12-08
  • C#WPF 自定义雷达图开发实例教程

    WPF 自定义雷达图开发实例教程

    这篇文章主要介绍了WPF 自定义雷达图开发实例教程,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下...

    WinterFish13112021-12-06