Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.chainofresponsibility
Interface Handler<R>

Type Parameters:
R - The request type handled by this handler.
All Known Subinterfaces:
HandlerChain<R>
All Known Implementing Classes:
AbstractHandlerChain, CharacterHandler, LetterCaseHandler, LetterHandler, StandardHandlerChain, SymbolHandler, WeakHandlerChain, WhitespaceHandler

@Participant(value="Handler")
public interface Handler<R>

A handler can handle a given type of request by taking appropriate action or by forwarding the request to the next handler in the current chain of handlers.

A handler is only linked to the next handler at execution time, and hence a handler can participate in several handler chains, which the original "Gang of Four" solution could not.

The actual code required to support the handler chain semantics is minimal in handler implementations:

   public Handler<R> handle(R request, HandlerLink<R> link) {
     // Unique for each handler implementation...
     if (.. request can be handled ..) {
       ..  
       return this;
     }  
     // Identical for *every* handler implementation...
     return link == null ? null : link.handle(request, this);  
   }
  
 

Author:
Gunni Rode / rode.dk

Method Summary
 Handler<R> handle(R request, HandlerLink<R> link)
          Handles the request supplied as request or forwards it to the next handler in the chain represented by the handler link supplied as link, if any.
 

Method Detail

handle

Handler<R> handle(R request,
                  HandlerLink<R> link)
Handles the request supplied as request or forwards it to the next handler in the chain represented by the handler link supplied as link, if any. link can be null, in which case this handler is considered the last handler in the chain.

If this handler handles request, this handler is returned. If this handler cannot handle request, the result from handling the request using link is returned. Finally, if link is null, null is returned.

Parameters:
request - The request to handle; nullability determined by the actual handler implementation.
link - The link to the next handler in the current chain; can be null.
Returns:
The handler that handled request, if any. Can be null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.