|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.chainofresponsibility.AbstractHandlerChain<R> dk.rode.thesis.chainofresponsibility.StandardHandlerChain<R>
R
- The type of requests handled by this chain.public class StandardHandlerChain<R>
A standard handler chain is the standard implementation
of the HandlerChain
interface.
A standard handler chain is not thread safe.
WeakHandlerChain
Field Summary | |
---|---|
private LinkedHashSet<Handler<R>> |
handlers
The ordered list of handlers, where the first handler is the first inserted element. |
Constructor Summary | |
---|---|
StandardHandlerChain()
No-arg constructor. |
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. |
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 class dk.rode.thesis.chainofresponsibility.AbstractHandlerChain |
---|
forward, handle, handle, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private final LinkedHashSet<Handler<R>> handlers
Implementation notes:
For small chains, a simple list will suffice, but it does
not scale well: if each handler starting from handler
m in the chain reinvokes the chain, we get a
running time of O(n*m), i.e. worst time O(n*n),
where n is the chain length. Existence, registration,
and unregistration takes O(n).
A better solution is to use a linked hash set to avoid O(n) existence, registration, and unregistration time. This is the implementation here.
An even faster and more scalable solution would be to use an index
into a list, pointing to the start of the sub-list to return.
A navigable
map (tree map) can be used
for this, i.e. tailMap(..)
methods. For this simple example,
we are satisfied with our simple solution.
Never null.
Constructor Detail |
---|
public StandardHandlerChain()
Method Detail |
---|
public List<Handler<R>> getHandlers(Handler<R> handler)
HandlerChain
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.
handler
- The handler to start from; can be null.
handler
; never null, but can be empty.public boolean isRegistered(Handler<R> handler)
HandlerChain
handler
is registered to this handler
chain, false if not.
handler
- The handler to test; cannot be null.
public void register(Handler<R> handler)
HandlerChain
handler
supplied
as handler
to this handler chain. Registering the same handler twice is an error.
handler
- The handler to add; cannot be null.HandlerChain.unregister(Handler)
public int size()
HandlerChain
public void unregister(Handler<R> handler)
HandlerChain
handler
supplied
as handler
from this handler chain. Trying to remove a handler that is not associated with this handler chain is an error.
handler
- The handler to remove; cannot be null.HandlerChain.register(Handler)
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |