• 设计模式学习笔记--职责链模式


     1 using System;
     2 
     3 namespace ChainOfResponsibility
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/1 6:54:00 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Handler说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public abstract class Handler
    12     {
    13         protected Handler successor;
    14 
    15         public void SetSuccessor(Handler successor)
    16         {
    17             this.successor = successor;
    18         }
    19 
    20         public abstract void HandleRequest(int request);
    21     }
    22 }
    View Code
     1 using System;
     2 
     3 namespace ChainOfResponsibility
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/1 6:56:18 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteHandler1说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteHandler1:Handler
    12     {
    13         public override void HandleRequest(int request)
    14         {
    15             if (request >= 0 && request < 10)
    16             {
    17                 Console.WriteLine("{0}处理请求{1}",this.GetType().Name,request);
    18             }
    19             else if(successor != null)
    20             {
    21                 successor.HandleRequest(request);
    22             }
    23         }
    24     }
    25 }
    View Code
     1 using System;
     2 
     3 namespace ChainOfResponsibility
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/1 6:59:24 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteHandler2说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteHandler2:Handler
    12     {
    13         public override void HandleRequest(int request)
    14         {
    15             if (request >= 10 && request < 20)
    16             {
    17                 Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request);
    18             }
    19             else if (successor != null)
    20             {
    21                 successor.HandleRequest(request);
    22             }
    23         }
    24     }
    25 }
    View Code
     1 using System;
     2 
     3 namespace ChainOfResponsibility
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/1 7:00:21 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteHandler3说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteHandler3:Handler
    12     {
    13         public override void HandleRequest(int request)
    14         {
    15             if (request >= 20 && request < 30)
    16             {
    17                 Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request);
    18             }
    19             else if (successor != null)
    20             {
    21                 successor.HandleRequest(request);
    22             }
    23         }
    24     }
    25 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ChainOfResponsibility
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Handler h1 = new ConcreteHandler1();
    14             Handler h2 = new ConcreteHandler2();
    15             Handler h3 = new ConcreteHandler3();
    16             h1.SetSuccessor(h2);
    17             h2.SetSuccessor(h3);
    18 
    19             int[] requests = { 2,5,14,22,18,3,27,20};
    20 
    21             foreach (int request in requests)
    22             {
    23                 h1.HandleRequest(request);
    24             }
    25         }
    26     }
    27 }
    View Code
  • 相关阅读:
    Dapr微服务应用开发系列1:环境配置
    酷炫的终端模拟器eDEX-UI
    man 命令显示简体中文帮助
    通过二维码传输文件到linux
    awk命令使用实例
    Centos安装vncserver虚拟网络控制台
    使用SSHFS文件系统远程挂载目录
    搜狗服务器引擎Sogou C++ Workflow开源啦!
    网站服务器系统,选windows还是linux?
    TikTok总部将留在美国 字节跳动保有控制权和核心算法
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5548302.html
Copyright © 2020-2023  润新知