DEV Community

Juarez Júnior for Develop4Us

Posted on • Edited on

C# Design Pattern: Chain of Responsibility

The Chain of Responsibility pattern is used to pass a request along a chain of handlers. Each handler decides whether to process the request or pass it to the next handler in the chain. It’s useful when you have multiple steps or responsibilities that can handle a request, like in an expense approval system, where different levels of managers can approve different amounts.

C# Code Example:

// Base class for handlers
public abstract class Approver
{
    protected Approver _successor;

    public void SetSuccessor(Approver successor)
    {
        _successor = successor;
    }

    public abstract void ProcessRequest(int amount);
}

// Low-level approver (can approve up to 1000)
public class Manager : Approver
{
    public override void ProcessRequest(int amount)
    {
        if (amount <= 1000)
        {
            Console.WriteLine($"Manager approved the request of {amount} dollars.");
        }
        else if (_successor != null)
        {
            _successor.ProcessRequest(amount);
        }
    }
}

// Mid-level approver (can approve up to 5000)
public class Director : Approver
{
    public override void ProcessRequest(int amount)
    {
        if (amount <= 5000)
        {
            Console.WriteLine($"Director approved the request of {amount} dollars.");
        }
        else if (_successor != null)
        {
            _successor.ProcessRequest(amount);
        }
    }
}

// High-level approver (can approve any amount)
public class President : Approver
{
    public override void ProcessRequest(int amount)
    {
        Console.WriteLine($"President approved the request of {amount} dollars.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating the chain of responsibility
        Approver manager = new Manager();
        Approver director = new Director();
        Approver president = new President();

        manager.SetSuccessor(director);
        director.SetSuccessor(president);

        // Testing the chain with different amounts
        manager.ProcessRequest(500);   // Manager approves
        manager.ProcessRequest(3000);  // Director approves
        manager.ProcessRequest(10000); // President approves
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have three levels of approvers: Manager, Director, and President. Each has a limit on the amount they can approve. If the amount exceeds the limit, the request is passed to the next approver in the chain. The Manager can approve up to 1000 dollars, the Director up to 5000, and the President can approve any amount.

Conclusion:

The Chain of Responsibility pattern allows different objects to attempt to process a request. It’s useful for systems where multiple steps or hierarchical levels need to process requests or make decisions. The request is passed along until someone can handle it.

Source code: GitHub

Top comments (0)