views:

249

answers:

1

Found myself trying to find a link to an official definition of this design pattern which I believe I saw in Go4 but can't seem to find it anywhere.

class Processor{
    ProcessParameter(AbstractParameter x){
     x.Process(this);
    }

    ProcessParameter(ParameterA x){
     ... A-specific logic...
    }

    ProcessParameter(ParameterB x){
     ... B-specific logic...
    }
}

abstract class AbstractParameter{
    abstract void Process(Processor p);
}

class ParameterA : AbstractParameter{
    override void Process(Processor p){
     p.ProcessParameter(this);
    }
}

class ParameterB : AbstractParameter{
    override void Process(Processor p){
     p.ProcessParameter(this);
    }
}
+12  A: 

It is the Visitor Pattern. The technique is called "double dispatch".

erickson
I liked the first version of your answer better. This _is_ Double Dispatch. The Visitor pattern adds the notion of _multiple_ Visitors (called "Processor" in my example).
zvolkov
I switched the emphasis since the title asks for the "pattern", and the question refers to GoF.
erickson