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
  • 相关阅读:
    8.JavaCC官方入门指南-例3
    7.JavaCC官方入门指南-例2
    6.JavaCC官方入门指南-例1
    5.JavaCC官方入门指南-概述
    4.JavaCC处理中文字符
    3.JavaCC 语法描述文件的格式解析
    2.idea安装JavaCC插件.md
    SQL 高效运行注意事项(二)
    视图用到的表更新表结构时注意刷新视图
    SQL 高效运行注意事项(一)
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5548302.html
Copyright © 2011-2022 走看看