`
chenying998179
  • 浏览: 25697 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Chain of Responsibility Pattern Example in C#

 
阅读更多

The Chain of Responsibility pattern takes a request and flows through a chain of handlers. Each handler checks if it can handle the request according to some business rules. If the handler can’t handle the request it gets passed to the next handler in the chain. The client doesn’t know which handler eventually handles the request.

In this example we send a parcel with a certain value. Some of the packages are insured. Based on the following criteria a package gets a certain treatment. For uninsured parcels with a value lower than 50 there is the budget treatment (you don’t want that). Parcels with a uninsured value from 50 and up get a normal treatment. All insured packages get a treatment deluxe. The chain of handlers is set up in that order.

Chain Of Responsibility Diagram

The base handler takes care of registering the next handler in the chain. A concreet implementation of a handler is show below. The ParcelHandlerBudget is the first in the chain of responsibility and checks if it can handle the parcel in the Handle() method. If so, we return a message with some hints to the kind of treatment the parcel is getting. If not, the NextHandler’s Handle() method is called.

namespace ChainOfResponsibilityPattern
{
    public class ParcelHandlerBudget : ParcelHandlerBase
    {
        public override string Handle(Parcel parcel)
        {
            if (parcel.Value < 50m && !parcel.IsInsured)
            {
                return "BUDGET: " +
                       "Assault package at a discount price";
            }

            return NextHandler.Handle(parcel);
        }
    }
}

In the client we create a parcel list with different (un)insured values and instantiate and chain up the parcel handlers. For every package on the list we walk up the chain of responsibility by calling Handle() on the first handler. The output of the ‘winning’ handler is returned and written to the console.

using System;
using System.Collections.Generic;

namespace ChainOfResponsibilityPattern
{
    internal class Program
    {
        private static void Main()
        {
            var parcelList =
                new List<Parcel>
                    {
                        new Parcel {IsInsured = false, Value = 2500m},
                        new Parcel {IsInsured = true, Value = 10m},
                        new Parcel {IsInsured = false, Value = 10m},
                        new Parcel {IsInsured = true, Value = 0m},
                        new Parcel {IsInsured = false, Value = 45m},
                        new Parcel {IsInsured = false, Value = 5m},
                        new Parcel {IsInsured = false, Value = 150m},
                    };

            var budgetHandler = new ParcelHandlerBudget();
            var normalHandler = new ParcelHandlerNormal();
            var deluxeHandler = new ParcelHandlerDeluxe();

            budgetHandler.SetNewHandler(normalHandler);
            normalHandler.SetNewHandler(deluxeHandler);

            foreach (Parcel parcel in parcelList)
            {
                try
                {
                    string result = budgetHandler.Handle(parcel);
                    Console.WriteLine(result);
                }
                catch (Exception)
                {
                    Console.WriteLine("Parcel rejected");
                }
            }

            Console.ReadKey();
        }
    }
}

Chain Of Responsibility Console

This design makes it easy to extend, change and maintain serial chain of responsibility rules. Further and recommended readings on the subject by Anoop Madhusudanan and Kory Becker.

分享到:
评论

相关推荐

    c++-设计模式之责任链模式(Chain of Responsibility Pattern)

    责任链模式(Chain of Responsibility Pattern)是一种行为型设计模式,它允许将请求的发送者和接收者解耦。通过将请求沿着处理链传递,直到有对象处理该请求为止,这种模式避免了请求发送者与具体处理者之间的紧...

    Chain of Responsibility模式

    **Chain of Responsibility模式详解** Chain of Responsibility模式是GOF(GoF,Gang of Four)在他们的经典著作《设计模式:可复用面向对象软件的基础》中提出的23种设计模式之一,属于行为模式类别。这个模式的...

    chain of responsibility.pptx

    责任链模式(Chain of Responsibility Pattern)是一种常见的行为模式。 使多个对象都有处理请求的机会,从而避免了请求的发送者和接收者之间的耦合关系。将这些对象串成一条链,并沿着这条链一直传递该请求,直到有...

    设计模式 - Chain of Responsibility Pattern(责任链模式)

    NULL 博文链接:https://linkcqu.iteye.com/blog/355806

    Chain of Responsibility 职责链模式

    C#面向对象设计模式 Chain of Responsibility 职责链模式 视频讲座下载

    Chain Of Responsibility模式

    **Chain of Responsibility 模式详解** Chain of Responsibility(责任链)模式是一种行为设计模式,它允许将请求沿着处理者对象的链进行发送,每个对象都可以处理请求或将其传递给链中的下一个对象。这种模式常...

    (行为型模式) Chain Of Responsibility 职责链模式

    C#面向对象设计模式 (行为型模式) Chain Of Responsibility 职责链模式 视频讲座下载

    C#面向对象设计模式纵横谈(20):(行为型模式) Chain Of Responsibility 职责链模式

    职责链模式(Chain of Responsibility)是一种行为型设计模式,它允许将请求沿着处理者对象的链进行传递,直到某个对象能够处理这个请求。在C#中,职责链模式的应用可以帮助我们构建灵活、可扩展的系统,减少对象...

    Design Pattern In c#

    如策略(Strategy)、模板方法(Template Method)、观察者(Observer)、命令(Command)、迭代器(Iterator)、访问者(Visitor)、备忘录(Memento)、状态(State)、职责链(Chain of Responsibility)和解释器...

    C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式)

    ### C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式) #### 概述 在本篇文章中,我们将深入探讨C#中的Chain of Responsibility(职责链)模式,这是行为型设计模式的一种。虽然标题...

    chain of responsibility 职责链模式(行为模式)

    职责链模式(Chain of Responsibility Pattern)是一种行为设计模式,它允许将请求的发送者和接收者解耦,使得多个对象都有可能处理一个请求,而无需显式指定接收者。在这个模式中,请求沿着一个处理者链进行传递,...

    C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式) (Level 300)

    职责链模式(Chain of Responsibility)是一种行为型设计模式,它允许你将请求沿着处理者对象的链进行传递,直到某个对象处理该请求。在C#编程中,职责链模式能够帮助我们实现一种松耦合的架构,使得请求的发送者和...

    设计模式C++学习之责任链模式(Chain of Responsibility)

    责任链模式(Chain of Responsibility)是一种行为设计模式,它允许将请求沿着处理者对象的链进行传递,直到某个对象能够处理这个请求。这种模式的主要优点是解耦了发送者和接收者之间的关系,使得系统更加灵活,...

    C#设计模式-吕震宇

    设计模式(17)-Chain of Responsibility Pattern 设计模式(16)-Bridge Pattern 设计模式(15)-Facade Pattern 设计模式(14)-Flyweight Pattern C#设计模式(13)-Proxy Pattern C#设计模式(12)-...

    Chain of Responsibility.zip

    设计模式-责任链的演示项目,导入eclipse中可直接运行,编程语言:java 设计模式学习请参考博客 https://www.li-tian.net.cn/archives/2020-01-27-23-01-15

    C#版 24种设计模式

    适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...

    Design Pattern in C#

    3. 行为型模式:包括策略模式(Strategy)、模板方法模式(Template Method)、观察者模式(Observer)、命令模式(Command)、责任链模式(Chain of Responsibility)、迭代器模式(Iterator)、访问者模式...

    java设计模式之责任链(chain of responsibility)

    责任链(Chain of Responsibility)模式是一种行为设计模式,它允许我们向对象链中传递请求,让每个对象决定是否处理这个请求。在Java中,这种模式常用于事件处理、日志系统或者权限控制等场景,其核心思想是将处理...

    【Java设计模式】(3)责任链Chain of Responsibility

    **Java设计模式——责任链(Chain of Responsibility)** 在软件工程中,设计模式是一种解决常见问题的可重用解决方案。责任链模式是行为设计模式之一,它的主要思想是将请求的发送者和接收者解耦,通过将多个处理...

Global site tag (gtag.js) - Google Analytics