欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

职责链模式

程序员文章站 2024-03-23 15:50:22
...

Chain of Responsibility

使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,将这个对象连接成一条链,并且沿着这条链传递该请求,直到有一个对象处理完它为止。

职责链模式

申请类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace China_of_Responsibility
{
    class Request
    {
        //申请类别
        private string requestType;
        public string RequestType
        {
            get { return requestType; }
            set { this.requestType = value; }
        }
        //申请内容
        private string requestContent;
        public string RequestContent
        {
            get { return requestContent; }
            set { requestContent = value; }
        }
        //申请数量
        private int number;
        public int Number
        {
            get { return number; }
            set { number = value; }
        }
    }
}

处理请求的抽象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace China_of_Responsibility
{
    //处理一个处理申请的接口
    abstract class Handler
    {
        protected Handler successor;
        //设置继任者
        public void SetSuccessor(Handler successor)
        {
            this.successor = successor;
        }
        public abstract void HandlerRequest(int request);
    }
}

处理请求的实际类1 2 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace China_of_Responsibility
{
    class ConcreteHandler : Handler
    {
        public override void HandlerRequest(int request)
        {
            if (request >= 0 && request < 10)
            {
                Console.WriteLine("{0} 处理请求 {1}",this.GetType().Name,request);
            }
            else if (successor != null)
            {
                successor.HandlerRequest(request);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace China_of_Responsibility
{
    class ConcreteHandler2 : Handler
    {
        public override void HandlerRequest(int request)
        {
            if (request >= 10 && request < 20)
            {
                Console.WriteLine("{0} 处理请求 {1}",this.GetType().Name,request);
            }
            else if (successor != null)
            {
                successor.HandlerRequest(request);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace China_of_Responsibility
{
    class ConcreteHandler3 : Handler
    {
        public override void HandlerRequest(int request)
        {
            if (request >= 20 && request < 30)
            {
                Console.WriteLine("{0} 处理请求 {1}",this.GetType().Name,request);
            }
            else if (successor != null)
            {
                successor.HandlerRequest(request);
            }
        }
    }
}

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace China_of_Responsibility
{
    class Program
    {
        static void Main(string[] args)
        {
            Handler h1 = new ConcreteHandler();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();
            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
            foreach (int request in requests)
            {
                h1.HandlerRequest(request);
            }
            Console.ReadKey();
        }
    }
}

当客户提交一个请求时候 请求是沿着链传递直至有一个ConcreteHandler对象负责处理它。请求者不用管那个对象去处理 反正该请求肯定会被处理。随时随地的增加或者修改处理一个请求的结构 增强了给对象指派职责的灵活性。但是要当心 一个请求有可能到了链的末端都得不到处理,或者因为没有正确的配置而得不到处理。