zoukankan      html  css  js  c++  java
  • 浅尝DesignPattern_ChainOfResponsibility

    UML:

    通过让多个对象都有机会处理该请求,来避免耦合请求的发送者的接收器,链接接收对象并且通过链来传递请求直到一个对象处理它

    Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. 

    Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. 

    • Handler   ()
      • defines an interface for handling the requests
      • 定义一个处理请求的接口
      • (optional) implements the successor link
      • 实现successor链接
    • ConcreteHandler   ()
      • handles requests it is responsible for
      • 处理负责的请求
      • can access its successor
      • 能够访问successor 
      • if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor
      • 如果ConcreteHandler能处理请求,它也可以.
    • Client   ()
      • initiates the request to a ConcreteHandler object on the chain  
      • 启动链上的请求

    Sample:

    abstract class Handler
    {
    protected Handler successor;
    public void SetSuccessor(Handler successor)
    {
    this.successor = successor;
    }
    public abstract void HandleRequest(int request);
    }
    class ConcreteHandler1:Handler
    {
    public override void HandleRequest(int request)
    {
    if (request >= 0 && request < 10)
    {
    Console.WriteLine(
    "{0} handled request {1}",
    this.GetType().Name, request);
    }
    else if (successor != null)
    {
    successor.HandleRequest(request);
    }
    }
    }
    class ConcreteHandler2:Handler
    {
    public override void HandleRequest(int request)
    {
    if (request >= 10 && request < 20)
    {
    Console.WriteLine(
    "{0} handled request {1}",
    this.GetType().Name, request);
    }
    else if (successor != null)
    {
    successor.HandleRequest(request);
    }
    }
    }
    class ConcreteHandler3:Handler
    {
    public override void HandleRequest(int request)
    {
    if (request >= 20 && request < 30)
    {
    Console.WriteLine(
    "{0} handled request {1}",
    this.GetType().Name, request);
    }
    else if (successor != null)
    {
    successor.HandleRequest(request);
    }
    }
    }
    #region ChainOR
    Handler h1
    = new ConcreteHandler1();
    Handler h2
    = new ConcreteHandler2();
    Handler h3
    = new ConcreteHandler3();
    h1.SetSuccessor(h2);
    h2.SetSuccessor(h3);

    int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
    foreach (int request in requests)
    {
    h1.HandleRequest(request);
    }
    #endregion
  • 相关阅读:
    【spring】基于AspectJ的AOP
    【matlab】stanford线性回归,logistic regression 实验
    【Python】列表、字典和元组的排序
    PHP 二叉树的深度优先与广度优先遍历
    PHP 定义栈结构,实现min函数,获取栈最小元素,要求时间复杂度为O(1)
    PHP 短连接生成
    一条SQL查询访问记录表(visit_log)中某个类目(catalog_id)的访问量(visit)排前两名的记录行
    利用 p, 1p 随机数发生器知道等概率发生器
    PHP 将二叉查找树转换为双向链表,要求不能创建新节点,只能调节节点指针
    PHP 求最大递增子序列长度
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1724741.html
Copyright © 2011-2022 走看看