Java Reference
In-Depth Information
a Request , dispatches a RequestHandler , and returns a Response object. With a
description in hand, you can code some simple starter interfaces, like those shown
in listing 3.1.
Listing 3.1 Request , Response , RequestHandler , and Controller interfaces
public interface Request
{
String getName();
}
public interface Response
{
}
public interface RequestHandler
{
Response process(Request request) throws Exception;
}
public interface Controller
{
Response processRequest(Request request);
void addHandler(Request request, RequestHandler requestHandler);
}
B
C
D
E
F
First, define a Request interface with a single getName method that returns the
request's unique name B , so you can differentiate one request from another. As
you develop the component, you'll need other methods, but you can add those as you
go along.
Next, specify an empty interface C . To begin coding, you need only return a
Response object. What the Response encloses is something you can deal with later. For
now, you need a Response type you can plug into a signature.
The next step is to define a RequestHandler that can process a Request and return
your Response D . RequestHandler is a helper component designed to do most of the
dirty work. It may call on classes that throw any type of exception. Exception is what
you have the process method throw.
Define a top-level method for processing an incoming request E . After accepting
the request, the controller dispatches it to the appropriate RequestHandler . Notice
that processRequest doesn't declare any exceptions. This method is at the top of the
control stack and should catch and cope with all errors internally. If it did throw an
exception, the error would usually go up to the Java Virtual Machine ( JVM ) or servlet
container. The JVM or container would then present the user with one of those nasty
white pages. It's better that you code for it yourself.
Finally, F is an important design element. The addHandler method allows you to
extend the Controller without modifying the Java source.
 
 
 
 
Search WWH ::




Custom Search