#3 Visitor pattern for QueueElementIF - handleEvent

open
nobody
None
5
2003-03-12
2003-03-12
No

Simple suggestion:
the QueueElementIF is, for the moment, only a tagging
interface. That means that, for any event handler, a
usual pattern is something like:
handleEvent(queueElementIF element) {
if (element instanceof SomeType) {
handleThatType((SomeType) element);
return;
}
//... and so on
}

We've come to the idea that a visitor pattern would be
much more elegant (a quick-and-dirty test showed that
no loss of time, nor gain; but code is simpler and more
elegant).

We could thus have:
* interface QueueElementIF {
public void visit(Object o);
}
(or Object could be replaced with a tagging Visitor
interface)

* interface ATCPVisitor { // or extends Visitor
public void visitATcpPacket(ATcpPacket packet);
public void visitATcpClose(ATcpClose closeEvent);
// ... and so on ...
}

* implementation of visit in ATcpPacket (and other
events) would only be:
public void visit(Object visitor) {
((ATcpVisitor)visitor).visitATcpPacket(this);
}

* handleEvent would simply be:
handleEvent(QueueElementIF element) {
element.visit(this);
}

As it appears that a refactoring of Seda is being
prepared, I think that this could be really useful.

Regards

Discussion