Chain of Responsibility pattern is a behavioral pattern that sends data (a request, a Command) to an concrete handler object and if that handler object can't use/handle it, it passes the data/request to other objects along the chain until an handler object handles it. ![]() source: GoF overviewThe Handler defines the interface required to handle request, while the ConcreteHandlers handle requests that they are responsible for. Think of Chain of Responsibility as Servlet Filters. In Chain of Responsibility, the Handler (or you may call it Chain) defines the interface required to handle a request (a Command), while each concrete handler object is supposed to handle the data/request that it is responsible for. Every node in the chain will have the responsibility to decide if they can serve the request/Command. If it can't, it'll then pass the data/request to the next concrete handler object in the chain. Once the request is handled, it completes it's journey through the chain. However, it is possible that no handler can handle the request. The chain sequence of the handler objects are set up in the client code by linking up one handler to another via setNextChain() as seen in the sample code below. chain of responsibility design pattern code exampleThe following source code was taken from New Think Tank by Derek Banas. Please visit his blog and support his awesome work. // The chain of responsibility pattern has a [1] Each Chain Handler decides whether it can serve the request. Client code: public class TestCalcChain { when to use chain of responsibility
references
0 Comments
Leave a Reply. |
Categories
All
Archives
May 2020
|