Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chain of Responsibility

Similar presentations


Presentation on theme: "Chain of Responsibility"— Presentation transcript:

1 Chain of Responsibility

2 A Day in IT-support Front-line Support Second-line Support
Internal support IT Issue (= easy) IT Issue (= hard) IT Issue (= very hard) Hmmmm… IT Issue (> hard) IT Issue (> very hard) IT Issue (> easy) IT Issue Front-line Support Manager Second-line Support Manager Internal Support Manager On-site Supporter RHS – SOC

3 A Day in IT-support Even though the involved managers are different, they essentially handle issues the same way: Can my department handle the issue? If yes, pass it to an actual handler in my department, and fix the issue If no, pass it to the next department manager RHS – SOC

4 Chain of Responsibility
In more abstract terms: We have a chain of Handlers available Each handler can handle a Request A Request is handled the same way by all Handlers: Can I handle the Request? If yes, then process it (and thereby consume it) If no, pass it on to the next handler RHS – SOC

5 Chain of Responsibility
Handler handleRequest(Request r) ConcreteHandlerA handleRequest(Request r) ConcreteHandlerB handleRequest(Request r) RHS – SOC

6 Chain of Responsibility
public void handleRequest(Request r) { if (canHandleRequest(r)) processRequest(r) else forwardRequest(r); } RHS – SOC

7 Chain of Responsibility
public void forwardRequest(Request r) { if (successor != null) successor.handleRequest(r) else throw (new UnhandledRequestException()); } RHS – SOC

8 Chain of Responsibility
Noice that a Handler has a reference to Handler – the successor in the chain of Handler objects One Handler does not know about details of other handlers Order of handlers can be changed dyna-mically, since handlers do not depend on each other RHS – SOC

9 Chain of Responsibility
Issues to consider in practice Can we be sure all requests are handled? What happens if a request is not handled by the final handler? Should the behavior of a handler depend on whether or not it has a successor? Who manages the chain of handlers during run-time? RHS – SOC

10 Chain of Responsibility
Issues to consider in practice How does a Handler actually decide, if it can handle a specific request Is it acceptable if two handlers can handle the same request? What if a request should return some value? RHS – SOC

11 Exercises Download the NetBeans project ChainOfResponsibilityExample from the Website (go to Classes, Week 43) Examine the code; the class Request defines a request, containing a request description text and a request difficulty (easy, medium, hard or unknown) The (abstract) class Handler defines the generic functionality for a request handler. However, the methods canHandleRequest and processRequest are abstract, and must be implemented in any concrete subclass Three concrete implementations of handlers are given as well, in the classes HandlerForEasyReq, HandlerForMediumReq and HandlerForHardReq. Examine them to determine which request types they can handle A HandlerManager is responsible for the initial setup of the chain of handlers The handlers are tested in the Main class – try it out! Experiment with adding in an additional handler for requests with Unknown difficulty, and remember to change the setup of the handler chain as well Also try to experiment with the order of handlers in the handler chain RHS – SOC


Download ppt "Chain of Responsibility"

Similar presentations


Ads by Google