Chain of Responsibility Pattern

Introduction and Implementation of Chain of Responsibility Design Pattern


Category: Design Patterns Tags: C#

Chain of Responsibility Pattern - Code Files

Introduction

    Chain of responsibility design pattern is behavioral design pattern. When a request has to be processed in chain of handlers we use this design pattern. Multiple handlers can be added to execute one after another and whichever handler fails in the chain doesn't forward the request to next handler and breaks the chain. Request is considered successfully processed only when last handler of the chain is successfully processed.

Think of a web application where a request has to go through request pipeline and there might be multiple checks before it hits controller like validation of headers, authorization, logging, notification etc.

Implementation

    Lets create an abstract processor which can process a request and hold pointer to the next handler in the chain.

public abstract class Processor
{
    protected readonly Processor? _nextProcessor;
    public Processor(Processor? nextProcessor = null)
    {
        _nextProcessor = nextProcessor;
    }

    public abstract bool Process();
}

Now I'm gonna extend this class to implement multiple handlers.

public class AuthorizationProcessor : Processor
{
    public AuthorizationProcessor(Processor? nextProcessor = null) : base(nextProcessor)
    {
    }

    public override bool Process()
    {
        // Process authentication
        Console.WriteLine("Authorization processed");

        if (_nextProcessor != null)
            return _nextProcessor.Process();

        return true;
    }
}

public class ValidationProcessor : Processor
{
    public ValidationProcessor(Processor? nextProcessor = null) : base(nextProcessor)
    {
    }

    public override bool Process()
    {
        // Process validations
        Console.WriteLine("Validation processed");

        if (_nextProcessor != null)
            return _nextProcessor.Process();

        return true;
    }
}

public class NotificationProcessor : Processor
{
    public NotificationProcessor(Processor? nextProcessor = null) : base(nextProcessor)
    {
    }

    public override bool Process()
    {
        // Process notification
        Console.WriteLine("Notification processed");

        if (_nextProcessor != null)
            return _nextProcessor.Process();

        return true;
    }
}

Since we have required handlers we can create chin out of these. First validation happens, next authorization and in the end we send notification that request is processed. Lets create chain and see output.

var notificationProcessor = new NotificationProcessor();
var authProcessor = new AuthorizationProcessor(notificationProcessor);
var validationProcessor = new ValidationProcessor(authProcessor);

validationProcessor.Process();

Output

Validation processed

Authorization processed

Notification processed

Chain of responsibility pattern make code simpler and maintainable by segregating the steps involved in processing a request.

 


Like 0 People
Last modified on 20 October 2022
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 164
Questions: 16
Given Best Solutions: 16 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x