CoR and Intercepting Filter

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
stryderjzw
Forum Newbie
Posts: 21
Joined: Sat Aug 06, 2005 5:45 pm

CoR and Intercepting Filter

Post 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!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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).
Post Reply