关于C# 设计模式 ChainOfResponsibility 职责链模式

关于C# 设计模式 ChainOfResponsibility 职责链模式

using System;
using System.Collections.Generic;
using System.Text;

///Chain of Responsibility(职责链,以下简称CoR)模式通过将多个对象串接成一条链(Chain),并沿着这条链传递上层应用传来的请求,
///直到有一个对象处理它为止,使得多个对象都有机会处理上层应用传来的请求,从而避免请求的发送者和接收者之间的耦合关系。
///add by jzh 2007-04-23
namespace DesignPattern
{
    abstract class Handler
    {
        protected Handler successorHandler;
        abstract public void HandleRequest(Request request);
        public void SetSuccessor(Handler sucessor)
        {
            successorHandler = sucessor;
        }
    }

    class ConcreteHandler1 : Handler
    {
        override public void HandleRequest(Request request)
        {
            if (request.RequestType == 1)
            {
                Console.WriteLine("Handler1 " + request.RequestParameters);
            }
            else
            {
                // next chain
                if (successorHandler != null)
                    successorHandler.HandleRequest(request);
            }
        }
    }

    class ConcreteHandler2 : Handler
    {
        override public void HandleRequest(Request request)
        {
            if (request.RequestType == 2)
            {
                Console.WriteLine("Handler2 " + request.RequestParameters);
            }
            else
            {
                // next chain
                if (successorHandler != null)
                    successorHandler.HandleRequest(request);
            }
        }
    }

    class ConcreteHandler3 : Handler
    {
        override public void HandleRequest(Request request)
        {
            if (request.RequestType == 3)
            {
                Console.WriteLine("Handler3 " + request.RequestParameters);
            }
            else
            {
                // next chain
                if (successorHandler != null)
                    successorHandler.HandleRequest(request);
            }
        }
    }

    class Request
    {
        private int iRequestType;
        private string strRequestParameters;

        public Request(int requestType, string requestParameters)
        {
            iRequestType = requestType;
            strRequestParameters = requestParameters;
        }

        public int RequestType
        {
            get
            {
                return iRequestType;
            }
            set
            {
                iRequestType = value;
            }
        }

        public string RequestParameters
        {
            get
            {
                return strRequestParameters;
            }
            set
            {
                strRequestParameters = value;
            }
        }
    }

    public class Client
    {
        public static void Main(string[] args)
        {
            Handler aHandler = new ConcreteHandler1();
            Handler bHandler = new ConcreteHandler2();
            Handler cHandler = new ConcreteHandler3();
            aHandler.SetSuccessor(bHandler);
            bHandler.SetSuccessor(cHandler);

            Request newRequest = new Request(2, "hi, jzh");
            aHandler.HandleRequest(newRequest);

            Console.ReadLine();
        }
    }
}

 

发表回复

您的电子邮箱地址不会被公开。