zoukankan      html  css  js  c++  java
  • 行为型设计模式之职责链模式(Chain of Responsibility)

    结构
    意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
    适用性
    • 有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
    • 你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
    • 可处理一个请求的对象集合应被动态指定。
      1 using System;
      2 
      3     abstract class Handler 
      4     {
      5         protected Handler successorHandler;
      6         abstract public void HandleRequest(Request request);        
      7         public void SetSuccessor(Handler sucessor)
      8         {
      9             successorHandler = sucessor;
     10         }
     11     }
     12 
     13     class ConcreteHandler1 : Handler
     14     {
     15         override public void HandleRequest(Request request)
     16         {
     17             // determine if we can handle the request
     18             if (request.RequestType == 1) // some complex decision making!
     19             {
     20                 // request handling code goes here
     21                 Console.WriteLine("request handled in ConcreteHandler1");
     22             }
     23             else 
     24             {
     25                 // not handled here - pass on to next in the chain
     26                 if (successorHandler != null)
     27                     successorHandler.HandleRequest(request);
     28             }
     29         }
     30     }
     31 
     32     class ConcreteHandler2 : Handler
     33     {
     34         override public void HandleRequest(Request request)
     35         {
     36             // determine if we can handle the request
     37             if (request.RequestType == 2) // some complex decision making!
     38             {
     39                 // request handling code goes here
     40                 Console.WriteLine("request handled in ConcreteHandler2");
     41             }
     42             else 
     43             {
     44                 // not handled here - pass on to next in the chain
     45                 if (successorHandler != null)
     46                     successorHandler.HandleRequest(request);
     47             }
     48         }
     49     }
     50 
     51     class ConcreteHandler3 : Handler
     52     {
     53         override public void HandleRequest(Request request)
     54         {
     55             // determine if we can handle the request
     56             if (request.RequestType == 3) // some complex decision making!
     57             {
     58                 // request handling code goes here
     59                 Console.WriteLine("request handled in ConcreteHandler3");
     60             }
     61             else 
     62             {
     63                 // not handled here - pass on to next in the chain
     64                 if (successorHandler != null)
     65                     successorHandler.HandleRequest(request);
     66             }        
     67         }
     68     }
     69 
     70     class Request 
     71     {
     72         private int iRequestType;
     73         private string strRequestParameters;
     74 
     75         public Request(int requestType, string requestParameters)
     76         {
     77             iRequestType = requestType;    
     78             strRequestParameters = requestParameters;
     79         }
     80 
     81         public int RequestType 
     82         {
     83             get 
     84             {
     85                 return iRequestType;
     86             }
     87             set 
     88             {
     89                 iRequestType = value;
     90             }
     91         }
     92     }
     93 
     94     /// <summary>
     95     ///    Summary description for Client.
     96     /// </summary>
     97     public class Client
     98     {
     99         public static int Main(string[] args)
    100         {
    101             // Set up chain (usually one need to be done once)
    102             Handler firstHandler = new ConcreteHandler1();
    103             Handler secondHandler = new ConcreteHandler2();
    104             Handler thirdHandler = new ConcreteHandler3();
    105             firstHandler.SetSuccessor(secondHandler);
    106             secondHandler.SetSuccessor(thirdHandler);
    107 
    108             // After setting up the chain of responsibility, we can
    109             // now generate requests and pass then off to the 
    110             // chain to be handled
    111 
    112             // generate and fire request
    113             Request newRequest = new Request(2,"This are the request parameters");
    114             firstHandler.HandleRequest(newRequest);
    115             
    116             return 0;
    117         }
    118     }
    职责链模式
  • 相关阅读:
    二、一切都是对象
    一、对象导论
    CSS 属性大全
    CSS颜色代码大全
    CSS Position 定位属性
    CSS Box Model 盒子模型
    ThreadLocal
    Java 模拟死锁
    byte 最小值为啥是最小是 -128 ;int最小值为啥是 -2147483648
    cmd 查看端口号占用情况
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/4666221.html
Copyright © 2011-2022 走看看