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

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

服务器之家 - 编程语言 - C# - C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例

C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例

2022-01-21 14:04GhostRider C#

这篇文章主要介绍了C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题,简单说明了责任链模式的概念,并结合《西游记》中真假美猴王故事背景为实例分析了责任链模式的具体使用技巧,需要的朋友可以参考下

本文实例讲述了c#设计模式之chainofresponsibility职责链模式解决真假美猴王问题。分享给大家供大家参考,具体如下:

一、理论定义

职责链模式 向一个 对象提出一个请求,如果这个对象无法处理这个请求,将指定下一个对象来处理这个请求, 直到这个请求能得到处理为止。

二、应用举例

需求描述:《西游记》里面的真假美猴王的辨别过程

六耳猕猴和孙悟空不仅外型一模一样,本事也是一模一样,走到哪儿,都无法分辨谁是真的谁是假的!

① 观音菩萨(guangyinbodhisattva)暗念《紧箍儿咒》,两个一齐喊疼,菩萨无计奈何。
② 李天王(litianwang)取照妖镜照住,镜中乃是两个孙悟空,毫发不差。玉帝亦辨不出。
③唐僧(tangseng)念《紧箍儿咒》,二人一齐叫苦,唐僧也不认得真假。
④ 阎罗殿的谛听(yanluodianditing)可以分辨的出真假,却不敢说出来。
⑤ 最后被如来(rulai)佛辨出真假,是所有神仙都没有听说过的新物种:六耳猕猴。

三、具体编码

1. 添加一个接口,定义一个处理方法,一个指向下一个神仙的属性。

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  /// <summary>
  /// 真 美猴王
  /// </summary>
  public enum therealmonkeyking {
    sunwukong=1,//孙悟空
    sixearmacaque=2//六耳猕猴
  }
  public interface ihandler
  {
    /// <summary>
    /// 判断真假美猴王
    /// </summary>
    /// <param name="sunwukong">孙悟空</param>
    /// <param name="sixearmacaque">六耳猕猴</param>
    /// <returns></returns>
    therealmonkeyking handler(string sunwukong, string sixearmacaque);
    /// <summary>
    /// 指定下一个神仙来判断真假美猴王
    /// </summary>
    ihandler nexthandler{get;set;}
  }
}

2. 神仙--观音菩萨

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  public class guangyinbodhisattva:ihandler
  {
    /// <summary>
    /// 观音菩萨 判断真假美猴王
    /// </summary>
    /// <param name="sunwukong">孙悟空</param>
    /// <param name="sixearmacaque">六耳猕猴</param>
    /// <returns></returns>
    public therealmonkeyking handler(string sunwukong, string sixearmacaque){
      console.writeline("观音菩萨暗念《紧箍儿咒》,两个一齐喊疼,菩萨无计奈何,突然想起李天王的照妖镜,对,交给他去辨别");
      return nexthandler.handler(sunwukong,sixearmacaque);
    }
    /// <summary>
    /// 指定下一路神仙 来判断真假美猴王
    /// </summary>
    public ihandler nexthandler { get; set; }
  }
}

3. 神仙--托塔李天王

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  public class litianwang : ihandler
  {
    /// <summary>
    /// 李天王 判断真假美猴王
    /// </summary>
    /// <param name="sunwukong">孙悟空</param>
    /// <param name="sixearmacaque">六耳猕猴</param>
    /// <returns></returns>
    public therealmonkeyking handler(string sunwukong, string sixearmacaque)
    {
      console.writeline("李天王取照妖镜照住,镜中乃是两个孙悟空,毫发不差。玉帝亦辨不出,两悟空大战几百回合,来到了唐僧那里");
      return nexthandler.handler(sunwukong, sixearmacaque);
    }
    /// <summary>
    /// 指定下一路神仙 来判断真假美猴王
    /// </summary>
    public ihandler nexthandler { get; set; }
  }
}

4. 神仙--唐僧(其实现在还不算神仙)

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  public class tangseng : ihandler
  {
    /// <summary>
    /// 唐僧 判断真假美猴王
    /// </summary>
    /// <param name="sunwukong">孙悟空</param>
    /// <param name="sixearmacaque">六耳猕猴</param>
    /// <returns></returns>
    public therealmonkeyking handler(string sunwukong, string sixearmacaque)
    {
      console.writeline(" 唐僧念《紧箍儿咒》,二人一齐叫苦, 唐僧也不认得真假,两悟空打到阎罗殿,谛听出来辨别");
      return nexthandler.handler(sunwukong, sixearmacaque);
    }
    /// <summary>
    /// 指定下一路神仙 来判断真假美猴王
    /// </summary>
    public ihandler nexthandler { get; set; }
  }
}

5. 神仙---阎罗殿谛听

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  public class yanluodianditing : ihandler
  {
    /// <summary>
    /// 阎罗殿的谛听 判断真假美猴王
    /// </summary>
    /// <param name="sunwukong">孙悟空</param>
    /// <param name="sixearmacaque">六耳猕猴</param>
    /// <returns></returns>
    public therealmonkeyking handler(string sunwukong, string sixearmacaque)
    {
      console.writeline("阎罗殿的谛听可以分辨的出真假, 却不敢说出来,因为六耳猕猴的后台很强:如来是也");
      return nexthandler.handler(sunwukong, sixearmacaque);
    }
    /// <summary>
    /// 指定下一路神仙 来判断真假美猴王
    /// </summary>
    public ihandler nexthandler { get; set; }
  }
}

6. 神仙--如来佛祖

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  public class rulai : ihandler
  {
    /// <summary>
    /// 唐僧 判断真假美猴王
    /// </summary>
    /// <param name="sunwukong">孙悟空</param>
    /// <param name="sixearmacaque">六耳猕猴</param>
    /// <returns></returns>
    public therealmonkeyking handler(string sunwukong, string sixearmacaque)
    {
      console.writeline(" 如来佛辨出真假,是所有神仙都没有听说过的新物种:六耳猕猴");
      //返回孙悟空,没有nexthandler了
      return therealmonkeyking.sunwukong;
      //还有人说应该返回therealmonkeyking.sixearmacaque,
      //因为六耳猕猴是如来佛的徒弟,用来除掉孙悟空,和菩提祖师斗智斗勇。这里打死的是真孙悟空。
      //另有一说六耳猕猴是孙悟空心魔,用佛洛伊德心理学分析,孙悟空也有反抗的一面,其实还是一个孙悟空。
      //还有一说是 六耳猕猴是真正的六耳猕猴,孙悟空也是真的孙悟空,
      //因为孙悟空打死的是 六耳猕猴的本相,六耳猕猴没必要变个猴子给人打死。
      //争议性还是蛮多。《西游记》还真是神作。
    }
    /// <summary>
    /// 指定下一路神仙 来判断真假美猴王
    /// </summary>
    public ihandler nexthandler { get; set; }
  }
}

7. 判断真假美猴王入口

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace com.design.gof.chainofresponsibility
{
  /// <summary>
  /// 判断真假美猴王入口
  /// </summary>
  public class judgementthekingofmonkey
  {
    //各路神仙都请来,放一起
    public ihandler guangyinbodhisattva { get; set; }
    public ihandler litianwang { get; set; }
    public ihandler tangseng { get; set; }
    public ihandler yanluodianditing { get; set; }
    public ihandler rulai { get; set; }
    /// <summary>
    /// 判断真假美猴王,首先从观音菩萨开始
    /// </summary>
    /// <returns></returns>
    public therealmonkeyking judge(string sunwukong,string sixearmacaque) {
     return guangyinbodhisattva.handler(sunwukong, sixearmacaque);
    }
  }
}

8. 下面是主函数测试

?
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
using system;
using system.collections.generic;
using system.linq;
using system.text;
using com.design.gof.observer;
using com.design.gof.chainofresponsibility;
namespace com.design.gof.test
{
  class program
  {
    static void main(string[] args)
    {
      #region chainofresponsibility
      //判断真假孙悟空,首先把各路神仙都new一下,让他们先喝茶休息
      judgementthekingofmonkey judgement = new judgementthekingofmonkey
      {
        guangyinbodhisattva = new guangyinbodhisattva(),//观音菩萨
        litianwang = new litianwang(),//托塔李天王
        tangseng = new tangseng(),//唐僧
        yanluodianditing = new yanluodianditing(),//阎罗殿谛听
        rulai = new rulai()//如来佛祖
      };
      //把各路神仙的责任划分一下,各自指定下一个神仙是谁
      //观音菩萨-->托塔李天王-->唐僧-->阎罗殿谛听-->如来佛祖
      judgement.guangyinbodhisattva.nexthandler = judgement.litianwang;
      judgement.litianwang.nexthandler = judgement.tangseng;
      judgement.tangseng.nexthandler = judgement.yanluodianditing;
      judgement.yanluodianditing.nexthandler = judgement.rulai;
      //两美猴王入场
      string sunwukong = "sunwukong", sixearmacaque = "sixearmacaque";
      //开始判断真假美猴王
      therealmonkeyking realmonkeyking = judgement.judge(sunwukong, sixearmacaque);
      console.writeline();
      console.writeline("then,真正的美猴王是:" + realmonkeyking + "孙悟空");
      #endregion
      console.readkey();
    }
  }
}

9. 运行结果

C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例

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

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

原文链接:http://www.cnblogs.com/HCCZX/archive/2012/07/31/2616608.html

延伸 · 阅读

精彩推荐
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08