I've seen an example implementation of the CoR pattern using a HashMap as the object that is passed down the chain, possibly with new content being added to it by handlers; an outline of the code below:
class HandlerImpl implements Handler {
Handler next;
void handle(HashMap context) {
// do handler logic, perhaps adding new stuff to "context"
if (next != null)
next.handle();
}
}
It's tempting to use, as handlers may enhance the context
with new information that may be used by following handlers without repeating the code. On the other hand, handlers become dependent on one another — they're still loosely coupled, but still, their order becomes increasingly important.
Is this code smell? If we find out that we cannot do with the CoR pattern without supplementing the context object with new information, what's the right pattern to use in such case?
I don't think passing a context down the chain is a violation of the pattern.
The servlet filter is usually used as an example for the CoR pattern, and filters are free to add/remove attributes on a shared request object or even to wrap the request in a new object in order to intercept any operation performed by subsequent filters or servlets.