Software

Chain of Responsibility Pattern

A chain of objects that are responsible for handling requests. Its a series of objects that are linked together.

Chain of Responsibility Visualization Diagram.png

When a client object sends a request, the first handler in the chain will try to process it. If a handler can process the request, the request ends at this handler. If a handler cannot deal with the request, the handler will send a request to the next handler nd so on. If any handler can process it, then the request if not satisfied.

The try/catch mechanism in C# or Java implements this design pattern.

Chain of Responsibility Class Diagram.png

Short algorithm how to write handlers properly:

  1. Check if rule matches.
  2. If it matches, do something specific.
  3. If it doesn’t match, call the next filter in the list.

To achieve this, can use the Template Method pattern where each request will handle the request in a similar way following the required steps.

One thought on “Chain of Responsibility Pattern

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.