我听过/读过这个词,但不太明白这意味着什么.
我什么时候应该使用这种技术,我将如何使用它?谁能提供一个好的代码示例?
我正在阅读有关访客模式的内容,它看起来像Double Dispatch.两者之间有什么区别吗?这两个术语是否意思相同.
任何人都可以详细解释在我的测试代码中print(Parent parent)
使用Child
实例时调用重载方法的原因吗?
这里涉及到Java中的任何虚拟方法或方法的重载/解决方案?有没有直接引用Java Lang Spec?哪个术语描述了这种行为?非常感谢.
public class InheritancePlay {
public static class Parent {
public void doJob(Worker worker) {
System.out.println("this is " + this.getClass().getName());
worker.print(this);
}
}
public static class Child extends Parent {
}
public static class Worker {
public void print(Parent parent) {
System.out.println("Why this method resolution happens?");
}
public void print(Child child) {
System.out.println("This is not called");
}
}
public static void main(String[] args) {
Child child = new Child();
Worker worker = …
Run Code Online (Sandbox Code Playgroud) 在下文中,我希望EventHandler以一种方式处理EventA,以另一种方式处理EventB,以及以另一种方式处理任何其他事件(EventC,EventD).EventReceiver仅接收对Event的引用并调用EventHandler.handle().当然,总是被调用的版本是EventHandler.handle(事件事件).
不使用instanceOf,有没有办法多态调度(可能通过EventHandler或泛型中的另一个方法)到适当的句柄方法?
class EventA extends Event {
}
class EventB extends Event {
}
class EventC extends Event {
}
class EventD extends Event {
}
class EventHandler {
void handle(EventA event) {
System.out.println("Handling EventA");
}
void handle(EventB event) {
System.out.println("Handling EventB");
}
void handle(Event event) {
System.out.println("Handling Event");
}
}
class EventReceiver {
private EventHandler handler;
void receive(Event event) {
handler.handle(event);
}
}
Run Code Online (Sandbox Code Playgroud) 我试着理解双重调度的工作原理.我创建了一个例子,其中一个怪物和一个来自抽象类生物的战士可以战斗.类Creature有方法"fight",它是在派生类中定义的,并且在每个派生类中定义了如果战士与战士或怪物等战斗会发生什么.我写了下面的代码:
#include<iostream>
using namespace std;
class Monster;
class Warrior;
class Creature{
public:
virtual void fight(Creature&) =0;
};
class Monster: public Creature{
void fightwho(Warrior& w) {cout<<"Monster versus Warrior"<<endl; }
void fightwho(Monster& m) {cout<<"Monster versus Monster"<<endl; }
public:
void fight(Creature& c) {c.fightwho(*this);}
};
class Warrior: public Creature{
void fightwho(Warrior& w) {cout<<"Warrior versus Warrior"<<endl; }
void fightwho(Monster& m) {cout<<"Monster versus Warrior"<<endl; }
public:
void fight(Creature& c) {c.fightwho(*this);}
};
int main()
{
Warrior w;
Monster m;
w.fight(m);
}
Run Code Online (Sandbox Code Playgroud)
这导致编译器错误,我预见到:
ex12_10.cpp:在成员函数'virtual void Monster :: fight(Creature&)':ex12_10.cpp:17:30:错误:'class …
我试图找到一种更好的方法来处理一些不断增长的if
构造来处理不同类型的类.这些类最终是包含不同值类型(int,DateTime等)的包装器,带有一些额外的状态信息.因此,这些类之间的主要区别在于它们包含的数据类型.虽然它们实现了通用接口,但它们也需要保存在同类集合中,因此它们还实现了非通用接口.类实例根据它们所代表的数据类型进行处理,并且它们的传播在此基础上继续或不继续.
虽然这不一定是.NET或C#问题,但我的代码是在C#中.
示例类:
interface ITimedValue {
TimeSpan TimeStamp { get; }
}
interface ITimedValue<T> : ITimedValue {
T Value { get; }
}
class NumericValue : ITimedValue<float> {
public TimeSpan TimeStamp { get; private set; }
public float Value { get; private set; }
}
class DateTimeValue : ITimedValue<DateTime> {
public TimeSpan TimeStamp { get; private set; }
public DateTime Value { get; private set; }
}
class NumericEvaluator {
public void Evaluate(IEnumerable<ITimedValue> values) ...
}
Run Code Online (Sandbox Code Playgroud)
我提出了两个选择:
双重发货 …
我有一个关于C++双重调度的问题.在下面的代码中,我希望第二组的结果与第一组的结果相匹配.
我不知道实际的类型(除非我尝试dynamic_cast)但我知道该对象继承自BaseClass类型.实现这一目标的最有效(性能)方法是什么?
谷歌搜索了一段时间后,我发现了双重调度和loki多方法.我在Shape示例中遇到的问题是,在我的应用程序中,Processor和BaseClass完全独立,并且没有可以相互调用的常用方法.其次,只有一个处理器(即没有从它继承).
谢谢你的帮助.
#include <iostream>
#include <string>
using namespace std;
class BaseClass{
public:
BaseClass(){}
virtual void myFunction(){cout << "base myFunction called" << endl;}
};
class Derived1: public BaseClass{
public:
Derived1():BaseClass(){}
void myFunction(){cout << "Derived1 myFunction called" << endl;}
};
class Derived2: public BaseClass{
public:
Derived2():BaseClass(){}
void myFunction(){cout << "Derived2 myFunction called" << endl;}
};
class Derived3: public BaseClass{
public:
Derived3():BaseClass(){}
void myFunction(){cout << "Derived3 myFunction called" << endl;}
};
class Processor{
public:
Processor(){}
virtual void processObj(BaseClass* bc){cout << "got …
Run Code Online (Sandbox Code Playgroud) c++ polymorphism design-patterns double-dispatch late-binding
我写了访客模式如下,但我不明白什么是单一和双重调度.AFAIK,单个调度是基于调用者类型调用方法,其中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* );
Run Code Online (Sandbox Code Playgroud)
请使用我提供的示例代码进行解释.
AFAIK,第一次调度发生在调用accept的对象上,第二次调度发生在调用visit方法的对象上.
谢谢.
double-dispatch ×10
c++ ×3
java ×3
c# ×2
oop ×2
polymorphism ×2
.net ×1
late-binding ×1
overloading ×1
visitor ×1