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

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

Type Parameters:
R - The type of requests handled by this chain.
All Superinterfaces:
Handler<R>, HandlerLink<R>
All Known Implementing Classes:
AbstractHandlerChain, StandardHandlerChain, WeakHandlerChain

public interface HandlerChain<R>
extends Handler<R>, HandlerLink<R>

A handler chain is an ordered list of handlers that will be passed a request in turn until the first applicable handler can handle it. To handle a given request, the handle(Object) method is used.

Any handler can register it self to a proper handler chain, and delegate the chain handling responsibility to the chain in case the handler cannot handle the request.

Handler chains can change at runtime.

Implementation notes:
The handler chain is Composite and allows other handler chains to be registered as handlers.

An alternative to using handler interfaces is to use annotations to mark handlers in a given class. The Observer pattern utilises such an approach to identify the methods (handlers) to notify relevant observers in the ObserverManager class.

Author:
Gunni Rode / rode.dk
See Also:
HandlerLink, ObservableSequence, CompositeSequence

Method Summary
 List<Handler<R>> getHandlers(Handler<R> handler)
          Returns an ordered list of the handlers currently associated with this chain, starting from the handler after handler, if supplied.
 Handler<R> handle(R request)
          Handles the request supplied as request by forwarding it to each handler in this chain until a handler can handle it, starting from the first registered handler.
 boolean isRegistered(Handler<R> handler)
          Returns true if handler is registered to this handler chain, false if not.
 void register(Handler<R> handler)
          Registers the handler supplied as handler to this handler chain.
 int size()
          Return the size of this handler chain.
 void unregister(Handler<R> handler)
          Removes the handler supplied as handler from this handler chain.
 
Methods inherited from interface dk.rode.thesis.chainofresponsibility.Handler
handle
 
Methods inherited from interface dk.rode.thesis.chainofresponsibility.HandlerLink
forward
 

Method Detail

getHandlers

List<Handler<R>> getHandlers(Handler<R> handler)
Returns an ordered list of the handlers currently associated with this chain, starting from the handler after handler, if supplied.

If handler is null, the entire list of handlers is returned. If handler is not registered to this chain, the entire list of handlers is returned.

Modifying the returned list will not affect this chain.

Parameters:
handler - The handler to start from; can be null.
Returns:
The ordered list of handlers, starting after handler; never null, but can be empty.

handle

Handler<R> handle(R request)
Handles the request supplied as request by forwarding it to each handler in this chain until a handler can handle it, starting from the first registered handler.

The handlers used are those delivered by getHandlers(Handler) with a null argument.

Parameters:
request - The request to handle. This chain allows it to be null, but individual handlers may not.
Returns:
The handler that handled request, if any; may be null.

isRegistered

boolean isRegistered(Handler<R> handler)
Returns true if handler is registered to this handler chain, false if not.

Parameters:
handler - The handler to test; cannot be null.
Returns:
True if registered, false if not.
Throws:
NullPointerException - If handler is null.

register

void register(Handler<R> handler)
Registers the handler supplied as handler to this handler chain.

Registering the same handler twice is an error.

Parameters:
handler - The handler to add; cannot be null.
Throws:
NullPointerException - If handler is null.
IllegalArgumentException - If handler is already registered to this chain.
See Also:
unregister(Handler)

size

int size()
Return the size of this handler chain.

Returns:
The size.

unregister

void unregister(Handler<R> handler)
Removes the handler supplied as handler from this handler chain.

Trying to remove a handler that is not associated with this handler chain is an error.

Parameters:
handler - The handler to remove; cannot be null.
Throws:
NullPointerException - If handler is null.
IllegalArgumentException - If handler is not registered to this chain.
See Also:
register(Handler)

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.