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
  • 相关阅读:
    angular 前端路由不生效解决方案
    LinqMethod 实现 LeftJoin
    Newtonsoft.Json 序列化踩坑之 IEnumerable
    Newtonsoft.Json 指定某个属性使用特定的时间格式
    [svc]Linux中Swap与Memory内存简单介绍
    [svc]Linux vmstat命令实战详解
    [svc]ansible自动化模块
    [svc]ssh+gg二步认证
    [svc][cpu][jk]cpu的核心查看及什么是cpu的负载
    [vt][xen]xenserver初始安装增加第二块硬盘&xen图形界面安装vm&设置xen里vm开机启动
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5548302.html
Copyright © 2011-2022 走看看