>You could also implement a "late-bound" visitor using a registry of
>type-to-processor-method mappings. Using such a registry, you could
>run the following code:

>  public void RegisterProcessor<T>(Processor<T> processor) {
>    processors.Add(typeof(T), delegate(object o) {
>      processor((T)o);
>    });
>  }

Nice! It solves the "big method" and I can only see one cast operation:
processor((T)o);

I tried something very similar but had problems with how to declare the
delegate but this method with the anonymous delegate method solves it.

One problem I see is if there are missing type-processor mappings, but
that's easily fixed:

public class ProcessorRegistry {

  ...

  public void Process(object o) {
    Type t = o.GetType();
    if (processor.ContainsKey(t))
        processors[t](o);
    else
           // throw exception, call a default processor or do nothing
  }


}

// Kristofer

===================================
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