A visitor template is a way to make duplicate submission in an object-oriented manner.
This is useful when you want to choose which method to use for a given argument based on its type at runtime rather than compilation time.
Double sending is a special case of sending multiple messages .
When you call a virtual method on an object that is considered unidirectional, because the actual method being called depends on the type of one object.
For double sending, the type of the object and the type of the only method method are taken into account. This is similar to allowing method overloads, except that the type of the argument is determined at runtime in dual dispatch instead of statically at compile time.
When sending multiple times, a method can have several arguments passed to it, and which implementation is used depends on each type of argument. The type evaluation procedure is language dependent. In LISP, it checks each type from first to last.
Languages ββwith multiple dispatchers use common functions that are simply functions of decartation and do not look like general methods that use type parameters.
To do double dispatch in C # , you can declare a method with a single object argument, and then specific methods with specific types:
using System.Linq; class DoubleDispatch { public T Foo<T>(object arg) { var method = from m in GetType().GetMethods() where m.Name == "Foo" && m.GetParameters().Length==1 && arg.GetType().IsAssignableFrom (m.GetParameters()[0].GetType()) && m.ReturnType == typeof(T) select m; return (T) method.Single().Invoke(this,new object[]{arg}); } public int Foo(int arg) { } static void Test() { object x = 5; Foo<int>(x);
Mark Cidade Sep 03 '08 at 21:38 2008-09-03 21:38
source share