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.
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();
}
}
}
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.
分享到:
相关推荐
23种设计模式之二十一(行为模式)Chain of Responsibility模式
责任链模式(Chain of Responsibility Pattern)是一种常见的行为模式。 使多个对象都有处理请求的机会,从而避免了请求的发送者和接收者之间的耦合关系。将这些对象串成一条链,并沿着这条链一直传递该请求,直到有...
NULL 博文链接:https://linkcqu.iteye.com/blog/355806
C#面向对象设计模式 Chain of Responsibility 职责链模式 视频讲座下载
C#面向对象设计模式纵横谈(20):(行为型模式) Chain Of Responsibility 职责链模式
Chain Of Responsibility.rar
C#面向对象设计模式 (行为型模式) Chain Of Responsibility 职责链模式 视频讲座下载
C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式) (Level 300)
C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式)
设计模式C++学习之责任链模式(Chain of Responsibility)
【Java设计模式】(3)责任链Chain of Responsibility源码
chain of responsibility 职责链模式(行为模式)来自于Microsoft官方网站视频教程,详细介绍了职责链模式的运用,且在本人提供的资源里可以下载到相关的PDF辅助文档帮助
NULL 博文链接:https://gary0416.iteye.com/blog/913457
设计模式(17)-Chain of Responsibility Pattern 设计模式(16)-Bridge Pattern 设计模式(15)-Facade Pattern 设计模式(14)-Flyweight Pattern C#设计模式(13)-Proxy Pattern C#设计模式(12)-...
设计模式-责任链的演示项目,导入eclipse中可直接运行,编程语言:java 设计模式学习请参考博客 https://www.li-tian.net.cn/archives/2020-01-27-23-01-15
适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...
创建型: 1. 单件模式(Singleton ... 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)
C#设计模式(23种设计...19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)
NULL 博文链接:https://stelin.iteye.com/blog/932101