码迷,mamicode.com
首页 > 其他好文 > 详细

责任链模式

时间:2015-06-19 11:45:51      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

1.定义

使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。责任链模式的重点是在"链"上,由一条链去处理相似的请求在链中决定谁来处理这个请求,并返回相应的结果。

技术分享

2.代码实现

    public abstract class Handler
    {
        private Handler _nextHandler;

        public Response HandleMessage(Request request)
        {
            Response response = null;
            //判断是否是自己的处理级别
            if (request.GetRequestLevel().Equals(this.GetHandlerLevel()))
            {
                response = Echo(request);
            }
            else
            {
//不属于自己的处理级别,判断是否有下一个处理者
if (_nextHandler != null) { response = _nextHandler.HandleMessage(request); } } return response; }
//设置下一个处理者
public void SetNextHandler(Handler handler) { _nextHandler = handler; } //每个处理者都必须实现处理任务 protected abstract Response Echo(Request request); //每个处理者都有一个处理级别 protected abstract Level GetHandlerLevel(); } public class ConcreteHandler1 : Handler { /// <summary> /// 定义自己的业务逻辑 /// </summary> /// <param name="request"></param> /// <returns></returns> protected override Response Echo(Request request) { throw new NotImplementedException(); } /// <summary> /// 设置自己的处理级别 /// </summary> /// <returns></returns> protected override Level GetHandlerLevel() { throw new NotImplementedException(); } } public class ConcreteHandler2 : Handler { /// <summary> /// 定义自己的业务逻辑 /// </summary> /// <param name="request"></param> /// <returns></returns> protected override Response Echo(Request request) { throw new NotImplementedException(); } /// <summary> /// 设置自己的处理级别 /// </summary> /// <returns></returns> protected override Level GetHandlerLevel() { throw new NotImplementedException(); } } public class ConcreteHandler3 : Handler { /// <summary> /// 定义自己的业务逻辑 /// </summary> /// <param name="request"></param> /// <returns></returns> protected override Response Echo(Request request) { throw new NotImplementedException(); } /// <summary> /// 设置自己的处理级别 /// </summary> /// <returns></returns> protected override Level GetHandlerLevel() { throw new NotImplementedException(); } }
    /// <summary>
    /// 请求与处理级别
    /// </summary>
    public class Level
    {

    }

    /// <summary>
    /// 定义一个请求
    /// </summary>
    public class Request
    {
        public Level GetRequestLevel()
        {
            return null;
        }
    }

    /// <summary>
    /// 处理者返回的数据
    /// </summary>
    public class Response
    {
    }
    /// <summary>
    /// 场景类
    /// </summary>
    class Program
    {

        private static void Main(string[] args)
        {
//声明所有的处理节点 Handler handler1
= new ConcreteHandler1(); Handler handler2 = new ConcreteHandler2(); Handler handler3 = new ConcreteHandler3(); //设置链中的阶段顺序 handler1.SetNextHandler(handler2); handler2.SetNextHandler(handler3); //提交请求,返回结果 handler1.HandleMessage(new Request()); Console.ReadKey(); } }

 

责任链模式

标签:

原文地址:http://www.cnblogs.com/JustYong/p/4588184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!