Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this pattern called (helps avoid type casting)?

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);
    }
}
like image 244
Stop Putin Stop War Avatar asked Apr 27 '09 17:04

Stop Putin Stop War


1 Answers

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

like image 147
erickson Avatar answered Sep 24 '22 23:09

erickson