Re: Problem applying generics to my code. Is there a better solution?



Daniel Pitts wrote:

Anytime that you find yourself using a chain of "instanceof" checks on
classes which you control, its time to look into using polymorphism
instead... You're particular problem may be best solved through a
Visitor pattern.

Small point, but "Visitor pattern" is an application of the broadly useful
technique of double-dispatch (which can be called a pattern too, if you like,
though it isn't often seen that way). In this case, I think, your suggestion
would be better seen as a direct application of double-dispatch, rather than an
application of Visitor -- and a better name could be found for the visit()
method (exposing what it /does/ rather than what feature of "Visitor" it
reflects).

A suggestion for the OP. Design and implement this stuff without using
generics at all, then go back and put in the necessary generic declarations.
Double dispatch -- or whatever -- is part of the /semantics/ of your system,
whereas generics are just a way of telling the typechecker what rules correct
applications of those semantics will follow. So unless generics make the
design/code easier to articulate in the first place (which I doubt in this
particular case), trying to keep the typechecker happy at the same time as
trying to think through the actual object interactions will be
counter-productive.

-- chris




.