Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Dispatcher eases workflow implementation

Improve your application's workflow with the Dispatcher design pattern and XSL

  • Print
  • Feedback
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.

  • Print
  • Feedback

Resources