zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--职责链模式

     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
  • 相关阅读:
    Elasticsearch常用命令
    Linux中使用systemctl操作服务、新建自定义服务
    Windows下安装MongoDB解压版
    Java执行cmd命令、bat脚本、linux命令,shell脚本等
    Ubuntu
    PostgreSQL删除数据库失败处理
    Ubuntu service 命令
    Ubuntu18修改/迁移mysql5.7数据存放路径
    攻防世界-web-ics-02(sql注入、ssrf、目录扫描)
    攻防世界-web-filemanager(源码泄漏、二次注入)
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5548302.html
Copyright © 2011-2022 走看看