zoukankan      html  css  js  c++  java
  • 设计模式のChainOfResponsibilityPattern(责任链模式)----行为模式

    一、产生背景

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

    二、实现方式

      职责链模式参与者:

      ◊ Handler

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

        ° 实现后继链

      ◊ ConcreteHandler

        ° 处理其所负责的请求

        ° 可访问其后继者

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

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

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

    三、实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 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,1,29 };
    
                foreach (int request in requests)
                {
                    h1.HandleRequest(request);
                }
    
                Console.ReadLine();
            }
        }
    
        public abstract class Handler
        {
            protected Handler successor;
    
            public void SetSuccessor(Handler successor)
            {
                this.successor = successor;
            }
    
            public abstract void HandleRequest(int request);
        }
    
        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);
                }
            }
        }
    
        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);
                }
            }
        }
    
        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);
                }
            }
        }
    }

    四、设计模式分析

    优点: 1、降低耦合度。它将请求的发送者和接收者解耦。 2、简化了对象。使得对象不需要知道链的结构。 3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。 4、增加新的请求处理类很方便。

    缺点: 1、不能保证请求一定被接收。 2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。 3、可能不容易观察运行时的特征,有碍于除错

  • 相关阅读:
    Codeforces Beta Round #92 (Div. 2 Only) B. Permutations 模拟
    POJ 3281 Dining 最大流 Dinic算法
    POJ 2441 Arrange the BUlls 状压DP
    URAL 1152 Faise Mirrors 状压DP 简单题
    URAL 1039 Anniversary Party 树形DP 水题
    URAL 1018 Binary Apple Tree 树形DP 好题 经典
    pytorch中的forward前向传播机制
    .data()与.detach()的区别
    Argparse模块
    pytorch代码调试工具
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/8744245.html
Copyright © 2011-2022 走看看