责任链模式

发布时间 2023-06-03 21:43:42作者: 云霄宇霁

The Chain of Responsibility design pattern avoids coupling the sender of the request to its receiver by giving more than one object a chance to handle the request. This pattern chains the receiving objects and passes the request along the chain until an object handles it.

责任链模式避免发送者和接受者耦合,给每个对象机会处理请求,此模式将接收对象连接起来,并沿着该链传递请求直到处理它为止。

In simple words, we can say that the chain of responsibility desgin pattern creates a chain of receiver objects for given request. In this design pattern, normally each receiver contains a reference to another receiver. If one receiver cannot handle the request then it passes the same request to the next receiver and so on.One receiver hanldes the request in the chain or one or more receivers handle the request.

 

UML Calss Diagram

 Handler: this is going to be an abstract class that deines how the request is going to be handled. It contains a member that holds the next handler in the chain and an associated method to set thhis next handler. It also has an abstract method that is going to be implemented by concrete handler classes to handle the incoming request as well as if required then it will pass the request to the next handler object in the pipeline. 

  ConcreteHandler: this is going to be concrete classes that is inherited from handler abstract class and provide implementations for the abstract method. This method has the logic to handle the request. And if required, then it will forward the request to the next handler associated tin the pipeline.

  Client: This is the class that generates the request and passes it to the first handler in the chain of responsibility.

 

Structure Code in C#

public abstract class Handler
    {
        protected Handler nextHandler;
        public void SetNextHandler(Handler nextHandler)
        { 
            this.nextHandler = nextHandler; 
        }
        public abstract void HandleRequest(int request);
    }
Handler
 public class ConcreteHanderA : Handler
    {
        public override void HandleRequest(int request)
        {
            if (request >= 0 && request < 10)
                Console.WriteLine($"{this.GetType().Name} handled request {request}.");
            else if (nextHandler != null)
                nextHandler.HandleRequest(request);
        }
    }

    public class ConcreteHanderB : Handler
    {
        public override void HandleRequest(int request)
        {
            if (request >= 10 && request < 20)
                Console.WriteLine($"{this.GetType().Name} handled request {request}.");
            else if (nextHandler != null)
                nextHandler.HandleRequest(request);
        }
    }

    public class ConcreteHanderC : Handler
    {
        public override void HandleRequest(int request)
        {
            if (request >= 20 && request < 30)
                Console.WriteLine($"{this.GetType().Name} handled request {request}.");
            else if (nextHandler != null)
                nextHandler.HandleRequest(request);
        }
    }
ConcreteHandler

When to use Chain of Responsibility Design Pattern in Real-Time Application?

  • A set of handlers are used in the pipeline to handle the request.
  • You need to pass a request to one handler at run-time based on certain conditions.
  • Exception Handling in C# is one of the best example of the chain for responsibility design pattern.When an exception is thrown from the try block, then that excpetion might be going to handle by a corresponding catch block. Here, you can have more than one catch block. Here the catch blocks will behave like handlers to handle exception.