The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.

Slides:



Advertisements
Similar presentations
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
Advertisements

Dispatching, monitoring, filtering
Chain of Responsibility Pattern Gof pp Yuyang Chen.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
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.
Zaki Alasadi Supervisor:Dr noorhosseini.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
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.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Agoracast: Design and Tools Review David Wallace Croft Senior Java Architect CroftSoft Inc Talk-Java/Drink-Java Las Colinas, Texas.
J2EE Structure & Definitions Catie Welsh CSE 432
Chapter 5 HTTP Request Headers. Content 1.Request headers 2.Reading Request Headers 3.Making a Table of All Request Headers 4.Sending Compressed Web Pages.
Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Java Servlets & Java Server Pages Lecture July 2013.
JSP Filters 23-Oct-15. JSP - FILTERS A filter is an object that can transform a request or modify a response. Filters are not servlets; they don't actually.
Pattern Oriented Design Chain of Responsibility From:Design Patterns Gamma. Johnson Helm. Vlissides (GoF) Present: F 楊汝康 R 徐德皓 R 高稚翔.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
® IBM Software Group © 2007 IBM Corporation Servlet Filtering
Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Servlet Filters JAVA Enterprise Edition. Servlet Filters Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes:
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Slide 1 Web Application Security ©SoftMoore Consulting.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Singleton Pattern (Creational)
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
MODEL VIEW CONTROLLER PATTERN. Model View Controller MVC is a time tested method of separating the user interface of an application from its Domain Logic.
Handling Errors in Web Applications
SQL Injection.
Java Servlets By: Tejashri Udavant..
Pre assessment Questions
Pre-assessment Questions
Jim Fawcett CSE776 – Design Patterns Summer 2003
Behavioral Patterns Part-I introduction UNIT-VI
Behavioral Design Pattern
Presentation transcript:

The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation Consider a context-sensitive help system for a graphical user interface. The object that ultimately provides the help isn’t known explicitly to the object (e.g., a button) that initiates the help request. We can use a chain of objects to decouple the senders from the receivers. The request gets passed along the chain until one of the objects handles it. Each object on the chain shares a common interface for handling requests and for accessing its successor on the chain. ©SoftMoore ConsultingSlide 2

Chain of Responsibility Pattern Intent: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Applicability: Use Chain of Responsibility when –more than one object may handle a request, and the handler isn’t known a priori. The handler should be ascertained dynamically at runtime. –you want to issue a request to one of several objects without specifying the receiver explicitly. –the set of objects that can handle a request should be specified dynamically. ©SoftMoore ConsultingSlide 3

Chain of Responsibility Pattern (continued) ©SoftMoore ConsultingSlide 4 Client Structure ConcreteHandler1 handleRequest() Handler handleRequest() ConcreteHandler2 handleRequest() successor

Chain of Responsibility Pattern (continued) Participants Handler –defines an interface for handling requests. –(optional) implements the successor link. ConcreteHandler –handles requests that it is responsible for. –can access its successor. –if it can handle the request, it does so; otherwise it forwards the request to its successor. Client –initiates the request to a ConcreteHandler object on the chain. ©SoftMoore ConsultingSlide 5

Chain of Responsibility Pattern (continued) Collaborations: When a client issues a request, the request propagates along the chain until a ConcreteHandler object takes responsibility for handling it. ©SoftMoore ConsultingSlide 6 : ConcreteHandler2: Client: ConcreteHandler1 handleRequest()

Chain of Responsibility Pattern (continued) Consequences Reduced coupling. The pattern frees an object from knowing which other object will handle a request. Both the receiver and the sender have no explicit knowledge of each other. Added flexibility in assigning responsibilities to objects. You can change the chain at run-time, and you can use subclassing to specialize handlers statically. Receipt isn’t guaranteed. Since a request has no explicit receiver, it can fall off the end of the chain without ever being handled. A request can also go unhandled when the chain is not configured properly. ©SoftMoore ConsultingSlide 7

Chain of Responsibility Pattern (continued) Implementation Two possible ways to implement the successor chain –define new links (e.g., in Handler) –use existing links (e.g., parent link in a composite) Allowing multiple handlers for the same request –a request can have zero, one, or several handlers in the chain The chain does not have to be linear; e.g., when using the parent link in a composite ©SoftMoore ConsultingSlide 8

Chain of Responsibility Pattern (continued) Implementation (continued) Representing requests –a “hard-coded” operation invocation convenient and safe permits only a fixed set of requests to be forwarded –request code (e.g., an integer constant or string) flexible, but less type-safe requires conditional statements for handling the request based on its code –use separate request objects ©SoftMoore ConsultingSlide 9

Chain of Responsibility Pattern in Java: Web Application Filters Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated with servlets, JSP pages, HTML pages, etc. Think of filters as a chain of steps that requests and responses must go through before reaching a servlet, JSP page, or HTML page. The same chain is traversed in reverse order before returning a response to the client (usually a browser). ©SoftMoore ConsultingSlide 10

Using Filters in Web Applications Filters can be used to query the request and act accordingly block the request/response from passing any further or reroute it to a different destination modify the request headers and data, providing a customized version of the request modify the response headers and data, providing a customized version of the response. ©SoftMoore ConsultingSlide 11

Filters in Action ©SoftMoore ConsultingSlide 12 Filter 1 HTML Page Filter 2Servlet Client (browser) HTTP Request HTTP Response Server Client (browser) HTTP Request HTTP Response

Possible Applications of Filters Authentication – blocking requests based on user identity. Logging and auditing – tracking users of a web application. Image conversion – scaling maps, and so on. Data compression – making downloads smaller. Localization – targeting the request and response to a particular locale. Transformations of XML content – targeting web application responses to more than one type of client. ©SoftMoore ConsultingSlide 13

Example: Logging User Access package com.softmoore.filters; import java.util.Date; public class LoggingFilter implements Filter { private FilterConfig filterConfig = null; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void destroy() { this.filterConfig = null; } ©SoftMoore ConsultingSlide 14

Example: Logging User Access (continued) public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) request; String message = req.getRemoteHost() + " accessed " + req.getRequestURL() + " on " + new Date() + "."); filterConfig.getServletContext().log(message); chain.doFilter(request,response); } ©SoftMoore ConsultingSlide 15

Example: Logging User Access (continued) In the deployment descriptor ( web.xml )... LoggingFilter com.softmoore.filters.LoggingFilter loggingFilter /admin/*... ©SoftMoore ConsultingSlide 16

Chain of Responsibility in Java AWT 1.0 The original version of Java AWT used the Chain of Responsibility pattern for event handling – the component where the event occurred could handle the event and/or pass it along to its container component, which could handle it and/or pass it along to its container. In Java 1.1 the AWT event model changed from the Chain of Responsibility pattern to the Observer pattern. –more efficient easier to ignore low-level events such as mouse movements –more flexible easier for non-GUI objects to handle events since handlers were not required to have a common superclass ©SoftMoore ConsultingSlide 17

Chain of Responsibility in Android Android uses the Chain of Responsibility pattern for event handling. UI components that are event sources have public callback methods that are called by the Android framework when the respective action occurs on that object. Events are dispatched starting at the top object in the view hierarchy and then down the hierarchy until they reach the appropriate destination. Most callback methods return a boolean value to indicate whether or not the event has been “consumed”. Slide 18©SoftMoore Consulting

Chain of Responsibility in Android (continued) The callback method returns true to indicate that the event has been handled (consumed) and that propagation of the event should stop at that point. Returning false indicates that the event has not been handled and that propagation should continue to other listeners in the hierarchy. Slide 19©SoftMoore Consulting

Related Patterns Chain of Responsibility is often applied in conjunction with Composite, where, a component’s parent can act as its successor. ©SoftMoore ConsultingSlide 20

References Chain-of-responsibility pattern (Wikipedia) Chain of Responsibility Pattern (Object-Oriented Design) Follow the Chain of Responsibility (David Geary, JavaWorld) BadInputFilter revisited (John Moore, JavaWorld) ©SoftMoore ConsultingSlide 21