Introduction
To tell the truth, I have seen Gof "design Patterns", has also been deeply Li Jianzhong "pattern" series Webcast. But I haven't seen "Double dispatch mode" yet. It is true that the design pattern mentioned by Gof is only a systematic introduction to the design pattern initially, and it cannot cover all patterns. In addition, with the passage of time, technology changes rapidly, the technical Daniel has summed up a lot of new models.
The double dispatch model introduced today is not a new design pattern in terms of time, but it is a new design pattern for technical colleagues who have only seen gof design patterns.
What is Doubledispatch?
Yes, there is no "pattern" word. In terms of literal translation, many people on the net translate to double distribution, double allocation. The interpretation of double dispatch on a wiki:
In software engineering, double dispatch are a special form of multiple dispatch, and a mechanism that dispatches a functio n call to different concrete functions depending on the runtime types of both objects involved in the call. In most object-oriented systems, the concrete function, which is called from a function call in the code depends on the Dyna Mic type of a single object and therefore they is known as single dispatch calls, or simply virtual function calls.
Effect:
In software engineering, Double Dispatch is a special form of multiple Dispatch and a mechanism for its corresponding concrete class (not a base class) method that is invoked based on the run-time type of two objects. In most object-oriented systems, the method in which a function in a code (program) invokes a concrete class depends on the dynamic type of the individual object (the type of runtime), which is called a single dispath calls, or just a virtual function call.
It is not difficult to see that the call of a virtual function is called a single dispath, then the double dispatch should be the call of the virtual function two times.
Further, it's just one time. A method function that invokes the true type of the corresponding subclass by a dynamic type (run-time type) is called a dispath. Then and so on, it takes two times to call a method function of the appropriate type through a run-time type, called a double Dispatch.
Example: Single Dispatch instance:
Doubdispatch instances:
Why do you want a double Dispatch?
Output results
How come there is no output of "Dog type" and "Mammals type"? Oh, no !!
How to solve? Use dynamic to solve
By looking at the instructions related to the function overload resolution we can learn:
Overloading is determined at compile time, so it cannot be dynamically determined at run time. Rewriting is the dynamic runtime decision.
Output Result:
Does this solution seem to solve the problem? But I don't realize it by double Dispatch. Just passed the dynamic.
Use Vistor mode to solve
Summarize
C # now introduces dynamic to support "Double Dispatch", but we should know exactly why this keyword is used.
If there is no such keyword, how does C # support "Dobule Dispatch", this is the purpose of this article.
More about the Vistor content, we can Baidu Google itself.
Follow-up I would like to write a blog about vistor, but also ask you to support a lot!
Reference
Design pattern Essay-make the mouth no longer difficult to adjust
Software design pattern
Design patterns not mentioned in Gof's writings (4):D ouble Dispatch
A little discussion on double Dispatch (the case is very thorough, C + + code)
Do what C + + can't do-visitor mode
Double Dispatch in C #?
Dynamic Dispatch in C #
Acyclic Visitor Mode: http://www.objectmentor.com/resources/articles/acv.pdf
Hierachical Visitor pattern Mode: Http://en.wikipedia.org/wiki/Hierarchical_visitor_pattern
Double Dispatch Explanation and example