JavaScript – Chain Of Responsibility Pattern

GitHub Code

The Chain Of Responsibility pattern serves as a pipeline through which any number of requests can be passed, but in a series of linked objects.  Very similar in concept to a linked list, each request is called and processed when its predecessor is done.  The ‘chain’ ends when the final request has been processed.

To run:
▪    Make a GET call

Screen Shot 2017-04-16 at 11.07.46 PM
▪    Review the results
Screen Shot 2017-04-16 at 11.07.21 PM

Due to the nature of JavaScript, I opted to use the Object.Create(arg) method to allow inheritance.  The ‘anatomy’ of this pattern is an abstract request which has a successor (next request to be processed), a name, a method for calling the next successor and then an over-ridable processRequest that the successor concrete implementation implements.
Screen Shot 2017-04-16 at 11.28.06 PM

The driver method creates the concrete request and then calls them.

Screen Shot 2017-04-16 at 11.28.16 PM
One thing to point out is that the ‘ingredients’ for most patterns revolve around abstract classes, interfaces and inheritance.  While not always true, most patterns I have interacted with have at least one or more of these elements.

Stay tuned!

References

  1. https://www.tutorialspoint.com/design_pattern/chain_of_responsibility_pattern.htm
  2. https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s