Author - StudySection Post Views - 10 views

Chain of Responsibility Pattern in C#

The Chain of Responsibility pattern in C# is a behavioral design pattern where a request is passed through a chain of handlers. Each handler decides either to process the request or to pass it to the next handler in the chain until the request is handled. This pattern promotes loose coupling between senders and receivers of a request. When the code is run, each request is processed by the appropriate handler based on its content. If no handler can process the request, a message indicating that none of the handlers can handle is displayed.

The key components of this pattern are as follows:

1. Handler Interface (IHandler): This interface defines the common methods that all handlers in the chain must implement. It typically includes a method like `HandleRequest`.

2. Concrete Handlers: These are the actual handler classes that implement the `IHandler` interface. Each concrete handler knows its successor (the next handler in the chain). They decide whether to handle the request or pass it to the next handler.

3. Client: The client initiates the request and sends it to the first handler in the chain. It doesn’t need to know the specific handler that will process the request or how the chain is structured.

Following is an example to illustrate how the Chain of Responsibility pattern works in C#:


using System;
// Request class
class Request
{
public string Content { get; set; }


public Request(string content)
{
Content = content;
}
}
// Handler interface
interface IHandler
{
void HandleRequest(Request request);
}
// Concrete Handler 1
class ConcreteHandler1 : IHandler
{
private IHandler _nextHandler;


public void SetNextHandler(IHandler handler)
{
_nextHandler = handler;
}
public void HandleRequest(Request request)
{
if (request.Content.Contains("handler1"))
{
Console.WriteLine("Request handled by Handler 1");
}
else if (_nextHandler != null)
{
_nextHandler.HandleRequest(request);
}
else
{
Console.WriteLine("Request cannot be handled.");
}
}
}


// Concrete Handler 2
class ConcreteHandler2 : IHandler
{
private IHandler _nextHandler;


public void SetNextHandler(IHandler handler)
{
_nextHandler = handler;
}
public void HandleRequest(Request request)
{
if (request.Content.Contains("handler2"))
{
Console.WriteLine("Request handled by Handler 2");
}
else if (_nextHandler != null)
{
_nextHandler.HandleRequest(request);
}
else
{
Console.WriteLine("Request cannot be handled.");
}
}
}


// Client code
class Client
{
public static void Main(string[] args)
{
// Creating handlers
var handler1 = new ConcreteHandler1();
var handler2 = new ConcreteHandler2();


// Chain the handlers
handler1.SetNextHandler(handler2);


// Creating requests
var request1 = new Request("This is a request for handler1");
var request2 = new Request("This is a request for handler2");
var request3 = new Request("This is a request that cannot be handled");

// Handling requests
handler1.HandleRequest(request1);
handler1.HandleRequest(request2);
handler1.HandleRequest(request3);
}
}

In the above example:

– Request class representing the requests that need to be handled.
– IHandler interface is defined with a HandleRequest method, ensuring that all concrete handlers implement this method.
– Two concrete handlers, ConcreteHandler1 and ConcreteHandler2, implement the IHandler interface. Each handler checks if it can handle the request based on its content. If it can’t handle it, it passes the request to the next handler in the chain.
– In the Client class, we create instances of concrete handlers, chain them together, and then send requests to the first handler.

This pattern helps achieve loose coupling between senders and receivers of a request, as the sender doesn’t need to know the specific receiver of this request. Additionally, it allows for dynamic modification of the chain during runtime.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri