Dispatcher eases workflow implementation
Improve your application's workflow with the Dispatcher design pattern and XSL
By Michael Ball, JavaWorld.com, 10/19/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
As a developer, you realize that requirements change as a natural part of the application development process. You can best
address such change by being prepared. In many applications, workflow -- the order in which a process proceeds -- experiences
frequent changes. In a Java 2 Platform, Enterprise Edition (J2EE) development environment, the Dispatcher design pattern defines
a strategy to determine workflow via a separate object. While the Dispatcher pattern, presented in the "
J2EE Patterns Catalog," helps consolidate workflow rules, it can also prove difficult to modify or reuse.
Therefore, application developers would benefit from an easily modifiable and reusable Dispatcher strategy. In this article,
you'll find techniques to develop a dynamic, configurable workflow tool designed to ease workflow development and maintenance,
as well as make the user's experience pleasant.
This article first introduces the Dispatcher pattern, as well as ancillary patterns such as Front Controller and Service to
Worker. The article then details an implementation strategy for a reusable dispatcher. It concludes with an example illustrating
how a dispatcher can help in a real-world business situation.
Note: This article features example source code.
The Dispatcher design pattern
The Dispatcher design pattern separates view responsibilities from business and system logic, and separates Web production
and software development teams. The Dispatcher pattern can meet the following needs:
- Dynamic content generation
- Business logic encapsulation in a camp other than the view
The Dispatcher pattern therefore becomes responsible for view management and navigation. It is best employed when outside
resources choose the view. For example, the Service-to-Worker pattern collects data before the dispatcher comes into use so
that the dispatcher can choose the view based on the data collected. That scenario contrasts with the typical dispatcher,
which chooses the view based on state information; in such a case, the view decides what data to pull.
Solutions
Developers most commonly employ the Dispatcher pattern with a request-handling controller. You could implement the controller
with a servlet, a JavaServer Page (JSP), or a component that delegates dispatch responsibilities to the Dispatcher object. Figure 1's sequence diagram demonstrates the process.

Figure 1. The Dispatcher pattern. (Source: Sun Microsystems Inc.) Click on thumbnail to view full-size image.
The sequence diagram in Figure 2 illustrates the Service-to-Worker pattern. Notice how it collects the data before the dispatcher
performs its tasks.

Figure 2. The Service-to-Worker pattern. (Source: Sun Microsystems Inc.) Click on thumbnail to view full-size image.
Strategies
You'll find many different strategies for implementing a dispatcher. Sun's "J2EE Patterns Catalog," proposes several:
- Servlet Front Strategy: A servlet determines workflow using the request, which the servlet then forwards to the appropriate view.
- JSP Front Strategy: A JSP determines workflow, then forwards the request to the view.
- JSP View Strategy: A combination of the JSP Front and the presentation logic. The JSP determines which view to use based on the request; this
can be achieved with something as simple as an if statement.
- Servlet View Strategy: Similar to the JSP View Strategy, except it employs servlets.
- JavaBean Helper Strategy: A JavaBean dispatches to the view based on the request.
- Custom Tag Helper Strategy: A tag library dispatches from a JSP.
- Dispatcher in Controller Strategy: The Controller performs the dispatching.
The XSL Dispatcher solution
Next, let's look at a dispatcher implemented using XML and XSL, in which XSL determines the view. The solution applies XSL
to an XML representation of the current session information, along with additional state information such as the current view,
the user's security role, and whatever other information might be handy.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download the source code to this article
http://www.javaworld.com/javaworld/jw-10-2001/dispatcher/jw-1019-dispatcher.zip
- "J2EE Patterns Catalog," Deepak Alur, John Crupi, and Dan Malks (Sun Microsystems Inc., March 2001) (free login required)
http://developer.java.sun.com/developer/technicalArticles/J2EE/patterns/
- The famous Gang of Four bookDesign Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley, 1995; ISBN0201633612)
http://www.amazon.com/exec/obidos/ASIN/0201633612/javaworld
- For a good introduction to Java-specific design patterns, read David Geary's Java Design Patterns column
http://www.javaworld.com/columns/jw-java-design-patterns-index.shtml
- For more design patterns stories, visit the Design Patterns section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-patterns-index.shtml
- For more XML stories, visit the Java and XML section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-xml-index.shtml
- Sign up for JavaWorld's free weekly email newsletters
http://www.idg.net/jw-subscribe
- You'll find a wealth of IT-related articles from our sister publications at IDG.net