zoukankan      html  css  js  c++  java
  • C#设计模式系列:职责链模式(Chain of Responsibility)

    1、职责链模式简介

    1.1>、定义

      职责链模式是一种行为模式,为解除请求的发送者和接收者之间的耦合,而使多个对象都有机会处理这个请求。将这些对象连接成一条链,并沿着这条链传递该请求,直到有一个对象处理它。

    1.2>、使用频率

       中低

    2、职责链模式结构

    2.1>、结构图

    2.2>、参与者

      职责链模式参与者:

      ◊ Handler

        ° 定义一个处理请求的接口

        ° 实现后继链

      ◊ ConcreteHandler

        ° 处理其所负责的请求

        ° 可访问其后继者

        ° 如果可处理该请求,则处理;否则将该请求转发给它的后继者。

      ◊ Client:向链上的具体处理者对象提交请求

      在职责链模式中,Client向Handler提交请求,请求在多个ConcreteHandler对象形成的对象链中被传递,请求在传递的过程中被处理。

    3、职责链模式结构实现

      Handler.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.ChainOfResponsibilityPattern.Structural
    {
        public abstract class Handler
        {
            protected Handler successor;
    
            public void SetSuccessor(Handler successor)
            {
                this.successor = successor;
            }
    
            public abstract void HandleRequest(int request);
        }
    }

      ConcreteHandler1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.ChainOfResponsibilityPattern.Structural
    {
        public 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);
                }
            }
        }
    }

      ConcreteHandler2.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.ChainOfResponsibilityPattern.Structural
    {
        public 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);
                }
            }
        }
    }

      ConcreteHandler3.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.ChainOfResponsibilityPattern.Structural
    {
        public 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);
                }
            }
        }
    }

      Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using DesignPatterns.ChainOfResponsibilityPattern.Structural;
    
    namespace DesignPatterns.ChainOfResponsibilityPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Setup Chain of Responsibility
                Handler h1 = new ConcreteHandler1();
                Handler h2 = new ConcreteHandler2();
                Handler h3 = new ConcreteHandler3();
    
                h1.SetSuccessor(h2);
                h2.SetSuccessor(h3);
    
                // Generate and process request
                int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
    
                foreach (int request in requests)
                {
                    h1.HandleRequest(request);
                }
            }
        }
    }

      运行输出:

    ConcreteHandler1 handled request 2
    ConcreteHandler1 handled request 5
    ConcreteHandler2 handled request 14
    ConcreteHandler3 handled request 22
    ConcreteHandler2 handled request 18
    ConcreteHandler1 handled request 3
    ConcreteHandler3 handled request 27
    ConcreteHandler3 handled request 20
    请按任意键继续. . .

      上例中,ConcreteHandler1负责处理的请求范围0~10,ConcreteHandler2负责处理的请求范围10~20,ConcreteHandler3负责处理的请求范围20~30。当请求ConcreteHandler1处理不了,则让ConcreteHandler2处理,如果ConcreteHandler2处理不了,则让ConcreteHandler3处理。依次类推,Client的请求会验证职责链传递下去,直至请求被处理,而Client不要关心到底是谁处理了请求。

    4、职责链模式应用分析

      职责链模式适用情形:

      1>、可能处理请求的对象集合以及它们在链表中的顺序是由Client根据当前应用的状态在运行时动态决定的;

      2>、Client根据状态,对于不同的请求类型,可以拥有不同的可能处理请求的对象集合。一个处理请求的对象也可以根据Client的状态和请求类型,把请求传递给不同的处理对象。

      3>、Client初始化请求,或者在不知道这些对象是否能处理这个请求的情况下初始化任何可能处理请求的对象。Client和在处理链表中的处理对象都不需要知道到底哪个对象去处理这个请求。

      4>、请求不能保证被处理。在没有处理的情况下,请求已经到达了处理链的表尾。

      职责链模式特点:

      1>、职责链模式降低了发出命令的对象和处理命令的对象之间的耦合,它允许多于一个的处理者对象根据自己的逻辑来决定哪个处理者最终处理这个命令。发出命令的对象只是把命令传给链结构的起始者,而不需要知道到底是链上的哪一个节点处理了这个命令。这样在处理命令上,允许系统由更多的灵活性。哪一个对象最终处理一个命令可以由那些对象参加职责链,以及随着这些对象在职责链上的位置不同而不同。

      2>、既然一个请求没有明确的接收者,那么就不能保证它一定会被处理。该请求可能一直到链的末端都得不到处理。一个请求也可以因该链没有被正确配置而得不到响应,并且处理消息传递和处理不当会出现消息的循环重复执行。

  • 相关阅读:
    各位信息技术男,你们在折腾什么?
    在 C# 中使用 P/Invoke 调用 Mupdf 函数库显示 PDF 文档
    用PDF补丁丁一分钟倒排逆页序PDF文档
    简单解决“ORA27100: shared memory realm already exists”的问题
    用PDF补丁丁一分钟批量更改PDF书签的默认展开或关闭状态
    TImageList 图标更新通知方案
    常用注释
    delphi XE 下打开内存泄漏调试功能
    炎炎夏日需要一个清凉的地 自制水冷系统(十一 指尖的思绪之程序篇)
    炎炎夏日需要一个清凉的地 自制水冷系统(十 高温作业:焊接)
  • 原文地址:https://www.cnblogs.com/libingql/p/3650611.html
Copyright © 2011-2022 走看看