Chain of Responsibility

Slides:



Advertisements
Similar presentations
Chain of Responsibility Pattern Gof pp Yuyang Chen.
Advertisements

Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Chapter 3: Arrays, Linked Lists, and Recursion
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Template method. RHS – SOC 2 How to make a pizza If you order a pizza, the manufacturing of a pizza goes through certain steps We can write up a sort.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;
Command. RHS – SOC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
Factory Patterns. RHS – SOC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage.
Composite A robot made of smaller robots.. * * Line with open diamond at the end means “aggregates”. Basically, it’s saying that the Component objects.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Introduction to Exceptions in Java CS201, SW Development Methods.
Memory Management.
Java Exceptions a quick review….
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Debugging Intermittent Issues
Writing simple Java Web Services using Eclipse
Building Java Programs
Java Programming Language
Debugging Intermittent Issues
Coding Defensively Coding Defensively
Programming Abstractions
Refactoring and Code Smells
SWE 332 Last Modified Spring 2010 Paul Ammann
C# Event Processing Model
Mocking Your Objects with Impunity
Chapter 12 Exception Handling and Text IO
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Exception Handling in Java
Chain of Responsibility
Linked Lists.
Fundamental Error Handling
Behavioral Patterns Part-I introduction UNIT-VI
Part B – Structured Exception Handling
Behavioral Design Pattern
Refactoring and Code Smells
Java Tutorial – Application Building
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
slides adapted from Marty Stepp and Hélène Martin
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Pointer analysis.
Introduction to AppInventor
Java Tutorial – Application Building
IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.
Refactoring and Code Smells
Interfaces.
slides created by Marty Stepp
Designing For Testability
Exceptions 10-May-19.
Building Java Programs
Why do we need exceptions?
Lecture 7: Linked List Basics reading: 16.2
Final Project.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Junit Tests.
Refactoring and Code Smells
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Chain of Responsibility

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

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

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

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

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

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

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

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

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

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