什么是单次和双次派遣? [英] What is Single and Double Dispatch?

查看:117
本文介绍了什么是单次和双次派遣?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了访问者模式如下,但我不明白什么是单和双调度。
AFAIK,单分派调用基于调用者类型的方法,其中双调度是基于调用者类型和参数类型调用一个方法。

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.

我猜想double dispatch是在单类层次结构中发生的,但是为什么visitor类有两个类层次结构,但仍然被认为是double dispatch。

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,第一个分派发生在调用accept的对象上,第二个分派发生在调用visit方法的对象上。

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

谢谢。

推荐答案

简单来说,单一分派就是当一个方法在一个参数的类型c> this )。双调度是两个参数的多态性。

In short, single dispatch is when a method is polymorphic on the type of one parameter (including the implicit this). Double dispatch is polymorphism on two parameters.

第一个典型的例子是一个标准的虚方法,它是包含对象类型的多态。

The typical example for the first one is a standard virtual method, which is polymorphic on the containing object's type. And the second one can be implemented via the Visitor pattern.

[update] 我假设在您的示例中, floppyDisk 处理器计算机每个继承自一个公共基类, $ c> accept 作为虚拟方法。类似地,访问* 方法应该在 equipmentVisitor 中声明为虚拟的,应该有一些派生类不同 visit * 实现。 [/ update]

[update] I assume that in your example, floppyDisk, processor and computer each inherit from a common base class which defines accept as a virtual method. Similarly, the visit* methods should be declared virtual in equipmentVisitor, which should have some derived classes with different visit* implementations. [/update]

假设如上, accept c $ c> this 和 equipmentVisitor 。 floppydisk,处理器和计算机都有自己的 accept 实现,所以当访问者调用 accept 时,cal是基于被调用者的类型调度。然后被调用者回调访问者的类型特定的访问方法,并且基于访问者的实际类型来调度该调用。

Assuming the above, accept is polymorphic on both this and equipmentVisitor. The floppydisk, processor and computer each have their own implementation of accept, so when the visitor calls accept, the cal is dispatched based on the type of the callee. Then the callee calls back the visitor's type specific visit method, and this call is dispatched based on the actual type of the visitor.

理论上可以有三元组,四元组等。dispatch too,虽然我从来没有见过这个在实践中实现(在不支持双重和更高的分派性质的语言,也就是 - 我似乎记住Smalltalk吗?)。使用C ++和类似语言中的Visitor进行双调度本身已经相当令人难以置信,因此实现三次调度和更高调度将非常复杂,无法在实际应用中使用。

In theory there can be triple, quadruple etc. dispatch too, although I have never seen this implemented in practice (in languages that don't support double and higher dispatches by nature, that is - I seem to remember that Smalltalk does?). Double dispatch using Visitor in C++ and similar languages is already quite mind-boggling in itself, so the implementation of triple and higher dispatches would simply be too complicated to be used in real applications.

这篇关于什么是单次和双次派遣?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆