Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Behavioural Design Pattern: Chain of Responsibility Design pattern

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:03:00

What are Behavioural Design Patterns?

Behavioral design patterns solve typical difficulties that arise while developing interactions between objects. These designs encourage better communication and collaboration between items.

Types of Behavioural Design Patterns

Following are the types of Behavioural Design Patterns:

  1. Chain of Responsibility
  2. Command
  3. Interpreter
  4. Iterator
  5. Mediator
  6. Memento
  7. Observer
  8. State
  9. Strategy
  10. Visitor
  11. Template Method

When to use the Behavioral Design Pattern?

Behavioural Design C# patterns are extremely useful for defining object interactions, managing algorithms, and encapsulating complex behavior. They provide an organized approach to managing tasks like as command execution, state changes, and iterative processes.

Define Chain of Responsibility Design pattern

The chain of responsibility pattern is used to execute a multi-step (series of steps) request process. The pattern separates the sender and receiver based on the type of request. In this design, each receiver (handler) typically contains a reference to another receiver. If one recipient cannot process the request, it is forwarded to the next receiver, and so on.

Examples of the Chain of Responsibility Pattern

The examples of the chain of responsibility pattern are:

  • Multi-Steps Request Process
  • Approval Process

Chain of Responsibility Pattern with UML Diagram

The classes, interfaces, & objects in the above UML class diagram are defined as follows.

  • Client: This class creates the request and sends it to the first handler in the chain of responsibility.
  • Handler: This abstract class provides a member that holds the next handler in the chain, as well as a method for setting this successor. It also provides an abstract function that needs to be implemented by concrete classes to handle the request or forward it to the next item in the pipeline.
  • ConcreteHandlerA and ConcreteHandlerB: These are concrete handler classes that derive from the Handler class. These include the ability to handle certain requests while passing others to the next item in the chain of requests.

When should you use the chain of responsibility pattern?

The chain of responsibility pattern is applicable in the following situations:

  • A collection of handlers for processing a request.
  • A scenario in which you must transmit a request to one of several handlers at runtime based on specific conditions.
  • The exception-handling system in C# is an excellent example of this pattern. In C#, any exception thrown by a piece of code is handled by a set of try-catch blocks. Catch blocks serve as possible exception handlers.

Advantages of the Chain of Responsibility Design Pattern

  • Decoupling: It separates the request sender from the receivers. The sender does not need to know which component in the chain will process the request.
  • Dynamic Handling: Handlers in the chain can be dynamically added or removed.
  • Flexibility: Provides greater flexibility in dividing duties among objects.
  • Responsibility Segregation: Responsibility Segregation allows for the division of processing or handling duties among various objects.

Use Cases of Responsibility Design Pattern

The Chain of Responsibility Design Pattern must be used in real-time applications under the following situations:

  • There are multiple handlers for a request.
  • Decoupling Request Senders and Receivers.
  • Conditional handling of requests.
  • Dynamic handling.
  • Putting Related Handlers Together.
  • Preventing direct coupling.
  • Creating an audit trail.
  • Complex validation and authorization processes.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this