Html/Javascript widget

Tuesday 23 August 2022

Chain of Responsibility

In design pattern, chain-of-responsibility pattern is a behavioral design pattern consisting of command objects and processing objects, each logic defining the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also adds new processing objects to the end of this chain.

This pattern promotes the idea of loose coupling.

The chain-of-responsibility pattern is nearly identical to the decorator pattern, the difference being that for the decorator, all classes handle the request, while for the chain of responsibility, exactly one of the classes in the chain handles the request .

Overview

The Chain of Responsibility design pattern is one of the twenty-three well-known GoF design patterns that describe common solutions to recurring design problems when designing flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

What problems can the Chain of Responsibility design pattern solve?

  • Coupling the sender of a request to its receiver should be avoided.
  • It should be possible that more than one receiver can handle a request.
Implementing a request directly within the class that sends the request is inflexible because it couples the class to a particular receiver and makes it impossible to support multiple receivers.

The solution that the Chain of Responsibility design pattern describes is for defining a chain of receiver objects having the responsibility, depending on run-time conditions, to either handle a request or forward it to the next receiver on the chain.

This enables us to send a request to a chain of receivers without having to know which one handles the request. The request gets passed along the chain until a receiver handles the request. The sender of a request is no longer coupled to a particular receiver.

No comments:

Post a Comment