> Sometimes I have to do a lot of typechecking to determine
> what to do based on the type of an object that looks like this:
>
> (Node is an abstract base class)
>
> void Process(Node node) {
>   if (node is BinaryNode) {
>      BinaryNode bnode = (BinaryNode)node;
>      Process(bnode.Left);
>      Process(bnode.Right);
>   }
>   else if (node is SetNode) {
>      SetNode snode = (SetNode)node;
>      foreach (Node n in snode.Nodes) {
>          Process(n);
>      }
>   }
>   else if (...) {
>      ...
>   }
> }

        This is all nice, but in the end you have to do something, otherwise 
it's just calling a tree recursively and that's it. :)
So your REAL process logic has to be placed inside the subclasses for the Nodes 
which sounds like a perfect candidate for the
strategy pattern.

        In Node you have an abstract method 'Process'. That one you override in 
all subclasses, and in there you add the node's REAL
process logic.

        In the BinaryNode's version, you simply call the real process logic 
twice, once for left and once for right. Etc.

                FB

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to