Page 1 of 1

CoR and Intercepting Filter

Posted: Fri Aug 19, 2005 1:00 am
by stryderjzw
Hi,

I'm wondering if anyone can explain the difference between Chain of Responsibility and Intercepting Filters... They both use some sort of "chains", but what's the logic difference there?

Thanks!

Posted: Fri Aug 19, 2005 12:14 pm
by McGruff
InterceptingFilter gets a mention here.

The GoF ChainOfResponsibility is a logical, decision-making patern. Think switch/case with classes. An event is passed along a series of handlers until one of them decides it can handle it, then chain execution stops. Only one handler is ever active.

Another type of chain might be a simple queue where all the chained objects fire, one after another.

InterceptingFilter is a mixture of the two. It's often seen in a FrontController and is used to load various pre and post processing tasks: input filters, authentication, logging - that kind of thing.

Posted: Fri Aug 19, 2005 1:48 pm
by timvw
With a Chain of Responsibility the objects in the chain have a chance to change the Request.
Here is an example that demonstrates this:

cat somefile | grep foo | cut -d " " f1 | sort | top > newfile

Each program (cat, grep, cut, sort, top) generates appropriate input for the following. Together they form a CoR to generate a newfile based on somefile.


In my understanding, Interception Filters all recieve the "same" input. (Well, apparently there are also examples where it's possible they can change the Request, which makes it hard to find an actual difference.)

Posted: Sat Aug 20, 2005 12:36 pm
by McGruff
That example is quite different to the chain described by the Gang of Four which is explicitly designed to "avoid coupling the sender of a request to its receiver" ie each chained object decides if it can handle the request and only one is ever active. The GoF pattern makes a decision about which operation to perform and only carries out one of the possible operations. A chain of objects forming some kind of processing queue applies each operation in turn.

Of course you can bend patterns into whatever shape you want.

Posted: Sat Aug 27, 2005 3:26 am
by Christopher
Both are chains of objects that passed along an object (or objects) to act upon. The difference is that every node in the Intercepting Filter chain acts upon the passed object; whereas with the CoR the object is passed along until a node "takes responsiblity" and acts upon the passed object (and then usually ends processing of the chain). The problem with the definitions is that often in an Intercepting Filter only one or a few nodes actually act, and with the modified CoR more than one node can "take responsiblity." So they end up being very similar in function, hence the confusion.

One difference that I think is probably a better way to differentiate them is that the Intercepting Filter chain is usually externally driven by a Filter Chain class, whereas the in the CoR is a linked list where each node is responsable for executing the next() node in the chain (and hence the ablity to break the chain).