Let's understand it literally. intuitively, it refers to two dispatches. What does dispatch mean here? For example:
Class Event
{
Public :
Virtual Void Printname ()
{
Cout < " I am a general event " < Endl;
}
}
Class Keyevent: Public Event
{
Public :
Virtual Void Printname ()
{
Cout < " I am a key event " < Endl;
}
}
Class Clickevent: Public Event
{
Public :
Virtual Void Printname ()
{
Cout < " Click events " < Endl;
}
}
Polymorphism is dynamic, and the called method is determined by the real type of the object. This process is called dispatch. For example, in C ++, each object has a virtual function table. When a base class type is used to reference a subclass object, the virtual function pointer points to the virtual function table of the subclass, all the called virtual functions are in the subclass version, so the followingCodeThe output is "I am a key event". This is a dispatch process, that is, the process of dynamically determining which function to call based on the object type.
Event*Pevent= NewKeyevent ();
Pevent->Printname ();
When Will two dispatches be used? Continue to look down:
Class Eventrecorder
{
Public :
Virtual Void Recordevent (Event * Event )
{
Cout < " Use eventrecorder to record general events " < Endl;
}
Virtual Void Recordevent (keyevent * Event )
{
Cout < " Use eventrecorder to record key events " < Endl;
}
Virtual Void Recordevent (clickevent * Event )
{
Cout < " Use eventrecorder to record click events " < Endl;
}
}
Class Advanceeventrecorder: Public Eventrecorder
{
Public :
Virtual Void Recordevent (Event * Event )
{
Cout < " Use advanced eventrecorder to record general events " < Endl;
}
Virtual Void Recordevent (keyevent * Event )
{
Cout < " Use advanced eventrecorder to record key events " < Endl;
}
Virtual Void Recordevent (clickevent * Event )
{
Cout < " Use advanced eventrecorder to record click events " < Endl;
}
}
These two classes contain three overload functions. polymorphism is dynamic, while function overload is static, which is determined during compilation. Therefore, the running result of the following code snippet is not what we expected:
Eventrecorder*Precorder= NewAdvanceeventrecorder ();
Event*Pevent= NewKeyevent ();
Precorder->Recordevent (pevent );
Output content: use advanced eventrecorder to record general events
In fact, what we expect to call in this scenario is: advanceeventrecorder: recordevent (keyevent * event)
Below we use the double dispatch design pattern to achieve the above code snippets, and add the following function to all event objects:
Virtual VoidRecordevent (eventrecorder*Recorder)
{
Recorder->Recordevent (This);
}
The following code snippet will be output: use advanced eventrecorder to record key events
Eventrecorder*Precorder= NewAdvanceeventrecorder ();
Event*Pevent= NewKeyevent ();
Pevent->Recordevent (precorder );
We can see that the first dispatch correctly found the keyevent recordevent (eventrecorder * recorder), and the second dispatch found the advanceeventrecorder recordevent (keyevent * event ).
The visitor mode is the application of double dispatch. In addition, in the collision detectionAlgorithmIt is also frequently used.