Besoin d'info sur pattern Chain of Responsibility - Java - Programmation
Marsh Posté le 26-10-2004 à 19:58:00
Voici un excellent articcle que te dira tout : http://www.javaworld.com/javaworld [...] terns.html
Marsh Posté le 26-10-2004 à 20:00:13
I recently switched to Mac OS X from Windows and I'm thrilled with the results. But then again, I only spent a short five-year stint on Windows NT and XP; before that I was strictly a Unix developer for 15 years, mostly on Sun Microsystems machines. I also was lucky enough to develop software under Nextstep, the lush Unix-based predecessor to Mac OS X, so I'm a little biased.
Aside from its beautiful Aqua user interface, Mac OS X is Unix, arguably the best operating system in existence. Unix has many cool features; one of the most well known is the pipe, which lets you create combinations of commands by piping one command's output to another's input. For example, suppose you want to list source files from the Struts source distribution that invoke or define a method named execute(). Here's one way to do that with a pipe:
grep "execute(" `find $STRUTS_SRC_DIR -name "*.java"` | awk -F: '{print $1}'
The grep command searches files for regular expressions; here, I use it to find occurrences of the string execute( in files unearthed by the find command. grep's output is piped into awk, which prints the first tokendelimited by a colonin each line of grep's output (a vertical bar signifies a pipe). That token is a filename, so I end up with a list of filenames that contain the string execute(.
Now that I have a list of filenames, I can use another pipe to sort the list:
grep "execute(" `find $STRUTS_SRC_DIR -name "*.java"` | awk -F: '{print $1}' | sort
This time, I've piped the list of filenames to sort. What if you want to know how many files contain the string execute(? It's easy with another pipe:
grep "execute(" `find $STRUTS_SRC_DIR -name "*.java"` | awk -F: '{print $1}' | sort -u | wc -l
The wc command counts words, lines, and bytes. In this case, I specified the -l option to count lines, one line for each file. I also added a -u option to sort to ensure uniqueness for each filename (the -u option filters out duplicates).
Pipes are powerful because they let you dynamically compose a chain of operations. Software systems often employ the equivalent of pipes (e.g., email filters or a set of filters for a servlet). At the heart of pipes and filters lies a design pattern: Chain of Responsibility (CoR).
Note: You can download this article's source code from Resources.
CoR introduction
The Chain of Responsibility pattern uses a chain of objects to handle a request, which is typically an event. Objects in the chain forward the request along the chain until one of the objects handles the event. Processing stops after an event is handled.
Figure 1 illustrates how the CoR pattern processes requests.
Figure 1. The Chain of Responsibility pattern
In Design Patterns, the authors describe the Chain of Responsibility pattern like this:
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.
The Chain of Responsibility pattern is applicable if:
* You want to decouple a request's sender and receiver
* Multiple objects, determined at runtime, are candidates to handle a request
* You don't want to specify handlers explicitly in your code
If you use the CoR pattern, remember:
* Only one object in the chain handles a request
* Some requests might not get handled
Those restrictions, of course, are for a classic CoR implementation. In practice, those rules are bent; for example, servlet filters are a CoR implementation that allows multiple filters to process an HTTP request.
Figure 2 shows a CoR pattern class diagram.
Figure 2. Chain of Responsibility class diagram
Typically, request handlers are extensions of a base class that maintains a reference to the next handler in the chain, known as the successor. The base class might implement handleRequest() like this:
public abstract class HandlerBase {
...
public void handleRequest(SomeRequestObject sro) {
if(successor != null)
successor.handleRequest(sro);
}
}
So by default, handlers pass the request to the next handler in the chain. A concrete extension of HandlerBase might look like this:
public class SpamFilter extends HandlerBase {
public void handleRequest(SomeRequestObject mailMessage) {
if(isSpam(mailMessage)) { // If the message is spam
// take spam-related action. Do not forward message.
}
else { // Message is not spam.
super.handleRequest(mailMessage); // Pass message to next filter in the chain.
}
}
}
The SpamFilter handles the request (presumably receipt of new email) if the message is spam, and therefore, the request goes no further; otherwise, trustworthy messages are passed to the next handler, presumably another email filter looking to weed them out. Eventually, the last filter in the chain might store the message after it passes muster by moving through several filters.
Note the hypothetical email filters discussed above are mutually exclusive: Ultimately, only one filter handles a request. You might opt to turn that inside out by letting multiple filters handle a single request, which is a better analogy to Unix pipes. Either way, the underlying engine is the CoR pattern.
In this article, I discuss two Chain of Responsibility pattern implementations: servlet filters, a popular CoR implementation that allows multiple filters to handle a request, and the original Abstract Window Toolkit (AWT) event model, an unpopular classic CoR implementation that was ultimately deprecated.
Servlet filters
In the Java 2 Platform, Enterprise Edition (J2EE)'s early days, some servlet containers provided a handy feature known as servlet chaining, whereby one could essentially apply a list of filters to a servlet. Servlet filters are popular because they're useful for security, compression, logging, and more. And, of course, you can compose a chain of filters to do some or all of those things depending on runtime conditions.
With the advent of the Java Servlet Specification version 2.3, filters became standard components. Unlike classic CoR, servlet filters allow multiple objects (filters) in a chain to handle a request.
Servlet filters are a powerful addition to J2EE. Also, from a design patterns standpoint, they provide an interesting twist: If you want to modify the request or the response, you use the Decorator pattern in addition to CoR. Figure 3 shows how servlet filters work.
Figure 3. Servlet filters at runtime
A simple servlet filter
You must do three things to filter a servlet:
* Implement a servlet
* Implement a filter
* Associate the filter and the servlet
Examples 1-3 perform all three steps in succession:
Example 1. A servlet
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class FilteredServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
PrintWriter out = response.getWriter();
out.println("Filtered Servlet invoked" );
}
}
Example 2. A filter
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class AuditFilter implements Filter {
private ServletContext app = null;
public void init(FilterConfig config) {
app = config.getServletContext();
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws java.io.IOException,
javax.servlet.ServletException {
app.log(((HttpServletRequest)request).getServletPath());
chain.doFilter(request, response);
}
public void destroy() { }
}
Example 3. The deployment descriptor
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
<filter>
<filter-name>auditFilter</filter-name>
<filter-class>AuditFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>auditFilter</filter-name>
<servlet-name>/filteredServlet</servlet-name>
</filter-mapping>
<!-- Servlet Configuration -->
<servlet>
<servlet-name>filteredServlet</servlet-name>
<servlet-class>FilteredServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>filteredServlet</servlet-name>
<url-pattern>/filteredServlet</url-pattern>
</servlet-mapping>
...
</web-app>
If you access the servlet with the URL /filteredServlet, the auditFilter gets a crack at the request before the servlet. AuditFilter.doFilter writes to the servlet container log file and calls chain.doFilter() to forward the request. Servlet filters are not required to call chain.doFilter(); if they don't, the request is not forwarded. I can add more filters, which would be invoked in the order they are declared in the preceding XML file.
Now that you've seen a simple filter, let's look at another filter that modifies the HTTP response.
Filter the response with the Decorator pattern
Unlike the preceding filter, some servlet filters need to modify the HTTP request or response. Interestingly enough, that task involves the Decorator pattern. I discussed the Decorator pattern in two previous Java Design Patterns articles: "Amaze Your Developer Friends with Design Patterns" and "Decorate Your Java Code."
Example 4 lists a filter that performs a simple search and replace in the body of the response. That filter decorates the servlet response and passes the decorator to the servlet. When the servlet finishes writing to the decorated response, the filter performs a search and replace within the response's content.
Example 4. A search and replace filter
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SearchAndReplaceFilter implements Filter {
private FilterConfig config;
public void init(FilterConfig config) { this.config = config; }
public FilterConfig getFilterConfig() { return config; }
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws java.io.IOException,
javax.servlet.ServletException {
StringWrapper wrapper = new StringWrapper((HttpServletResponse)response);
chain.doFilter(request, wrapper);
String responseString = wrapper.toString();
String search = config.getInitParameter("search" );
String replace = config.getInitParameter("replace" );
if(search == null || replace == null)
return; // Parameters not set properly
int index = responseString.indexOf(search);
if(index != -1) {
String beforeReplace = responseString.substring(0, index);
String afterReplace=responseString.substring(index + search.length());
response.getWriter().print(beforeReplace + replace + afterReplace);
}
}
public void destroy() {
config = null;
}
}
The preceding filter looks for filter init parameters named search and replace; if they are defined, the filter replaces the first occurrence of the search parameter value with the replace parameter value.
SearchAndReplaceFilter.doFilter() wraps (or decorates) the response object with a wrapper (decorator) that stands in for the response. When SearchAndReplaceFilter.doFilter() calls chain.doFilter() to forward the request, it passes the wrapper instead of the original response. The request is forwarded to the servlet, which generates the response.
When chain.doFilter() returns, the servlet is done with the request, so I go to work. First, I check for the search and replace filter parameters; if present, I obtain the string associated with the response wrapper, which is the response content. Then I make the substitution and print it back to the response.
Example 5 lists the StringWrapper class.
Example 5. A decorator
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class StringWrapper extends HttpServletResponseWrapper {
StringWriter writer = new StringWriter();
public StringWrapper(HttpServletResponse response) { super(response); }
public PrintWriter getWriter() { return new PrintWriter(writer); }
public String toString() { return writer.toString(); }
}
StringWrapper, which decorates the HTTP response in Example 4, is an extension of HttpServletResponseWrapper, which spares us the drudgery of creating a decorator base class for decorating HTTP responses. HttpServletResponseWrapper ultimately implements the ServletResponse interface, so instances of HttpServletResponseWrapper can be passed to any method expecting a ServletResponse object. That's why SearchAndReplaceFilter.doFilter() can call chain.doFilter(request, wrapper) instead of chain.doFilter(request, response).
Now that we have a filter and a response wrapper, let's associate the filter with a URL pattern and specify search and replace patterns:
Example 6. A deployment descriptor
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
...
<filter>
<filter-name>searchAndReplaceFilter</filter-name>
<filter-class>SearchAndReplaceFilter</filter-class>
<init-param>
<param-name>search</param-name>
<param-value>Blue Road Inc.</param-value>
</init-param>
<init-param>
<param-name>replace</param-name>
<param-value>Red Rocks Inc.</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>searchAndReplaceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
I've now wired the search and replace filter to all requests by associating it with the URL pattern /* and specified that Red Rocks Inc. will replace the first occurrence of Blue Road Inc.. Let's try it on this JavaServer Pages (JSP) page:
Example 7. A JSP page
Welcome to Blue Road Inc.
Figure 4 shows the preceding JSP page's output. Notice that Red Rocks Inc. has replaced Blue Road Inc..
Figure 4. Using a search and replace filter. Click on thumbnail to view full-size image.
Of course, my search and replace filter serves little practical use other than demonstrating how filters can modify the response with a wrapper. However, useful freely available servlet filters are easy to find. For example, Tomcat 4.1.X comes with the following filters: a compression filter that zips responses larger than a threshold (that you can set as a filter parameter); an HTML filter; and a filter that dumps information about a request. You can find those filters under Tomcat's examples directory.
Servlet filters represent a popular CoR pattern variation where multiple objects in the chain may handle a request. Let's wrap up the CoR pattern with a brief look at a classic CoR implementation that was deprecated.
The AWT event model
The AWT originally used the CoR pattern for event handling. This is how it worked:
import java.applet.Applet;
import java.awt.*;
public class MouseSensor extends Frame {
public static void main(String[] args) {
MouseSensor ms = new MouseSensor();
ms.setBounds(10,10,200,200);
ms.show();
}
public MouseSensor() {
setLayout(new BorderLayout());
add(new MouseSensorCanvas(), "Center" );
}
}
class MouseSensorCanvas extends Canvas {
public boolean mouseUp(Event event, int x, int y) {
System.out.println("mouse up" );
return true; // Event has been handled. Do not propagate to container.
}
public boolean mouseDown(Event event, int x, int y) {
System.out.println("mouse down" );
return true; // Event has been handled. Do not propagate to container.
}
}
The preceding application creates a canvas and adds it to the application. That canvas handles mouse up and down events by overriding mouseUp() and mouseDown(), respectively. Notice those methods return a boolean value: true signifies that the event has been handled, and therefore should not be propagated to the component's container; false means the event was not fully handled and should be propagated. Events bubble up the component hierarchy until a component handles it; or the event is ignored if no component is interested. This is a classic Chain of Responsibility implementation.
Using the CoR pattern for event handling was doomed to failure because event handling requires subclassing components. Because an average graphical user interface (GUI) uses many components, and most components are interested in at least one event (and some are interested in many events), AWT developers had to implement numerous component subclasses. Generally, requiring inheritance to implement a heavily used feature is poor design, because it results in an explosion of subclasses.
The original AWT event model was eventually replaced with the Observer pattern, known as the delegation model, which eliminated the CoR pattern. Here's how it works:
import java.awt.*;
import java.awt.event.*;
public class MouseSensor extends Frame {
public static void main(String[] args) {
MouseSensor ms = new MouseSensor();
ms.setBounds(10,10,200,200);
ms.show();
}
public MouseSensor() {
Canvas canvas = new Canvas();
canvas.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("mouse down" );
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouse up" );
}
});
setLayout(new BorderLayout());
add(canvas, "Center" );
}
}
The delegation modelwhere a component delegates event handling to another objectdoesn't require extending component classes, which is a much simpler solution. With the delegation event model, event methods return void because events are no longer automatically propagated to a component's container.
The CoR pattern was not applicable for AWT events because GUI events are much too fine-grained; because there are so many events, and components handle them so frequently, the CoR pattern resulted in an explosion of subclassesan average GUI could easily have upwards of 50 component subclasses for the single purpose of handling events. Finally, propagation of events was rarely used; typically, events are handled by the component in which they originated.
On the other hand, the CoR pattern is a perfect fit for servlet filters. Compared to GUI events, HTTP requests occur infrequently, so the CoR pattern can better handle them. And event propagation is very useful for filters because you can combine themmuch like the Unix pipes discussed at the beginning of this articleto produce a myriad of effects.
Last link
The Chain of Responsibility pattern lets you decouple an event's sender from its receiver with a chain of objects that are candidates to handle the event. With the classic CoR pattern, one or none of the objects in the chain handles the event; if an object doesn't handle the event, it forwards it to the next object in the chain. In this article, we examined two CoR pattern implementations: the original AWT event model and servlet filters.
About the author
David Geary is the author of Core JSTL Mastering the JSP Standard Tag Library (Prentice Hall, 2002; ISBN: 0131001531), Advanced JavaServer Pages (Prentice Hall, 2001; ISBN: 0130307041), and the Graphic Java series (Prentice Hall). David has been developing object-oriented software with numerous object-oriented languages for almost 20 years. Since reading the GOF Design Patterns book in 1994, David has been an active proponent of design patterns, and has used and implemented design patterns in Smalltalk, C++, and Java. In 1997, David began working full-time as an author and occasional speaker and consultant. David is a member of the expert groups defining the JSP Standard Tag Library and JavaServer Faces, and is a contributor to the Apache Struts JSP framework. David is currently working on Core JavaServer Faces, which will be published in the spring of 2004.
Resources
* Download this article's associated source code:
http://www.javaworld.com/javaworld [...] tterns.zip
* The famous Gang of Four book: Design Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley Publishing Co., 1995; ISBN: 0201633612):
http://www.amazon.com/exec/obidos/ [...] /javaworld
* David Geary discussed the Decorator pattern in two previous Java Design Patterns columns:
o "Amaze Your Developer Friends with Design Patterns" (October 2001):
http://www.javaworld.com/javaworld [...] terns.html
o "Decorate Your Java Code" (December 2001):
http://www.javaworld.com/javaworld [...] terns.html
* For details about the Observer design pattern, see "An Inside View of Observer," David Geary (JavaWorld, March 2003):
http://www.javaworld.com/javaworld [...] terns.html
* Read David Geary's other Java Design Patterns columns:
http://www.javaworld.com/columns/j [...] ndex.shtml
* For more design pattern articles, visit the Design Patterns section of JavaWorld's Topical Index:
http://www.javaworld.com/channel_c [...] ndex.shtml
* Jason Hunter's JavaWorld articles about Servlet 2.3 examine the servlet filter model:
o "Servlet 2.3: New Features Exposed" (January 2001):
http://www.javaworld.com/javaworld [...] etapi.html
o "Filter Code with Servlet 2.3 Model" (June 2001):
http://www.javaworld.com/javaworld [...] lters.html
* Also read about the latest Servlet 2.4 specification in Jason Hunter's "Servlet 2.4: What's in Store" (JavaWorld, March 2003):
http://www.javaworld.com/javaworld [...] rvlet.html
* For more servlet-related articles, see the Servlets section of JavaWorld's Topical Index:
http://www.javaworld.com/channel_c [...] ndex.shtml
* Browse the AWT/Swing section of JavaWorld's Topical Index:
http://www.javaworld.com/channel_c [...] ndex.shtml
* Browse the JavaServer Pages section of JavaWorld's Topical Index:
http://www.javaworld.com/channel_c [...] ndex.shtml
* Browse the Java 2 Platform, Enterprise Edition (J2EE) section of JavaWorld's Topical Index:
http://www.javaworld.com/channel_c [...] ndex.shtml
* Speak out in the Enterprise Java discussion:
http://www.javaworld.com/javaforum [...] rprisejava
* Sign up for JavaWorld's free weekly email newsletters:
http://www.javaworld.com/subscribe
Marsh Posté le 26-10-2004 à 20:00:36
ReplyMarsh Posté le 26-10-2004 à 20:04:12
Merci les gars c'est super sympa d'avoir répondu si vite mais j'ai oublié de préciser que je ne pige pas grand chose en anglais et pour dire la vérité en anglais non plus.
J'aid éjà fait pas mal de recherche sur google et mon exposé et presque bouclé, sauf que comme j'y capte pas grand chose je n'arrive pas à donner un avis et j'ai pas trouver de synonymes...
Merci pour votre aide!!!
Marsh Posté le 26-10-2004 à 20:19:06
oui oui oui, bon, on ferme hein ! tu fais un exposé recopié sur gogole auquel tu comprends que dalle, tant mieux pour toi, mais ton topic est hors charte !
(et file apprendre l'anglais)
Marsh Posté le 26-10-2004 à 19:56:16
Bonjour,
Je cherche un max d'informations (pour un exposé) sur le pattern Chain of Responsibility. Si vous pouviez me transmettre des documents ce serait super sympa ******
J'ai surtout besoin d'en connaître les synonymes et d'avoir l'avis de quelqu'un qui s'y connaisse sur ce pattern.
Merci d'avance et à bientôt!!!!
Message édité par virgile06 le 26-10-2004 à 20:19:50
---------------
Austin Mini, Mini Moke et autres dérivés.