David: To parse TCP header information, the base class would have to know about its child.
I think what Ernest was trying to refer to is to use polymorphism to write various overloaded methods for different childs. You can call the same method for every object you receive, the relevant method will be called based on the instance of the object. (
Polymorphic functions)
Infact this is what is a Visitor pattern!
David: your last idea seems off the mark for my project. Once the needed information from a Packet is obtained, the Packet is discarded.
Again, what Ernest meant was that in the map you put an instance of
java.lang.Class as the key and value as the parser implementation for that class. When you get an object instance call
Object.getClass() method on the instance. Then lookup the parser implementation from the map based on the looked up class instance.
In my personal opinion, this case is an absolute fit for the visitor pattern if you can not change the class implementations to introduce a parse() method in either the base class(an abstract method) or use an interface.
[ February 25, 2008: Message edited by: Nitesh Kant ]
[ February 26, 2008: Message edited by: Nitesh Kant ]