Design Patterns not mentioned in the gof book (4): Double dispatch

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.