Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Single and Double Dispatch?

i have wrote the visitor pattern as follow but i don't understand what is single and double dispatch. AFAIK, single dispatch is invoke a method based on caller type where double dispatch is invoke a method based on caller type and argument type.

I guess double dispatch is happen in single class hierarchy but why visitor class has two class hierarchy but it still considered as double dispatch.

void floppyDisk::accept(equipmentVisitor* visitor)
{
 visitor->visitFloppyDisk(this);
}

void processor::accept(equipmentVisitor* visitor)
{
 visitor->visitProcessor(this);
}

void computer::accept(equipmentVisitor* visitor)
{
 BOOST_FOREACH(equipment* anEquip, cont)
 {
  anEquip->accept(visitor);
 }

 visitor->visitComputer(this);
}

void visitFloppyDisk(floppyDisk* );
void visitProcessor(processor* );
void visitComputer(computer* );

Please explain using the example code i provided.

AFAIK, the first dispatch is happen on object who invoke the accept and second dispatch is happen on object who invoke the visit method.

Thanks.

like image 947
nicholas Avatar asked Jul 16 '10 07:07

nicholas


People also ask

What is single dispatch?

single dispatch (plural single dispatches) (computing) A dispatch method where the implementation of a function or method is chosen solely on the type of the instance calling the method.

What is double dispatch in programming?

In software engineering, double dispatch is a special form of multiple dispatch, and a mechanism that dispatches a function call to different concrete functions depending on the runtime types of two objects involved in the call.

What is double dispatch in visitor pattern?

In “double dispatch”, the operation executed depends on: the name of the request, and the type of TWO receivers (the type of the Visitor and the type of the element it visits). This essentially means different visitors can visit the same type and different types can be visited by the same visitor.

What is double dispatch Java?

Double dispatch is a technical term to describe the process of choosing the method to invoke based both on receiver and argument types. A lot of developers often confuse double dispatch with Strategy Pattern. Java doesn't support double dispatch, but there are techniques we can employ to overcome this limitation.


1 Answers

In your example, you are missing the basics of the mechanism: inheritance and virtuality. Let's assume the following class hierarchy, in addition to your code:

class equipmentVisited
{
  virtual void accept(equipmentVisitor* visitor) = 0;
}

class floppyDisk : public equipmentVisited
{
  virtual void accept(equipmentVisitor* visitor);
}

class processor : public equipmentVisited
{
  virtual void accept(equipmentVisitor* visitor);
}

class computer : public equipmentVisited
{
  virtual void accept(equipmentVisitor* visitor);
}

class equipmentVisitor
{
  virtual void visitFloppyDisk(floppyDisk* );
  virtual void visitProcessor(processor* );
  virtual void visitComputer(computer* );
}

// Some additional classes inheriting from equipmentVisitor would be here

Now, imagine you have this piece of code in some function:

equipmentVisited* visited;
equipmentVisitor* visitor;
// ...
// Here you initialise visited and visitor in any convenient way
// ...
visited->accept(visitor);

Thanks to the double dispatch mechanism, this last line allows any equipmentVisited to accept any equipmentVisitor, no matter what their actual static types are. Eventually, the right function will be called for the right class.

To summarise:

  • The first dispatch calls accept() on the appropriate class
  • The second dispatch calls the appropriate function on the class selected by the first dispatch
like image 173
Gorpik Avatar answered Oct 07 '22 11:10

Gorpik