Introduction: Java is a single dispatch language that supports dual dispatch
Know that Java is to support late dynamic binding, but also know the visitor pattern, but the relationship between the two really do not know, and there is a static binding between this.
1. Dynamic binding
classFather { Public voidmethod () {System.out.println ("This is Father ' s method"); }} classSon1extendsfather{ Public voidmethod () {System.out.println ("This is Son1 ' s method"); }} classSon2extendsfather{ Public voidmethod () {System.out.println ("This is Son2 ' s method"); }} Public classTest { Public Static voidMain (string[] args) {Father S1=NewSon1 (); S1.method (); Father S2=NewSon2 (); S2.method (); }}
Printing results:
This is Son1′s method
This is Son2′s method
Using Java, the results above will be dismissive. So look at the following example, anyway, I'm dumbfounded.
2. Static bindings
classFather {}classSon1extendsFather {}classSon2extendsFather {}classExecute { Public voidmethod (Father Father) {System.out.println ("This is Father ' s method"); } Public voidmethod (Son1 son) {System.out.println ("This is Son1 ' s method"); } Public voidmethod (Son2 son) {System.out.println ("This is Son2 ' s method"); }}
It's a magical discovery, and the results are printed.
This is Father ' s method
This is Father ' s method
This is Father ' s method
For overloading, it is only at compile time that it has been decided. is to select which overloaded method to invoke specifically based on the declaration object, so that all methods of the parent class are called
Of course, we can deal with the instanceof keyword, but this way, if there are more subclasses, so the logic will be very awkward to judge, and do not add a subclass, you need to add an if else otherwise, the code will be problematic
In this way, it is understood that Java is a single language. So how do you understand and support the two factions? The answer is that Java can implement a dual-faction mechanism through visitor patterns
First, let's look at a simple example
classvisitor_a { Public voidmethod1 () {System.out.println ("Visitor_a dosomething"); } Public voidmethod2 (Visitor_b B) {B.calla ( This); }}classVisitor_b { Public voidCalla (visitor_a A) {a.method1 (); }} Public Static voidMain (string[] args) {visitor_a visitor_a=Newvisitor_a (); Visitor_b Visitor_b=NewVisitor_b (); Visitor_a.method1 (); Visitor_b.calla (visitor_a);}
Above, is the visitor pattern of the most basic form, through observer B to access a interface, to achieve the dual ability, in fact, is also dependent on the nature of Java's late dynamic binding mechanism capabilities
classFather { Public voidAccept (Execute exe) {exe.method ( This); }}classSon1extendsfather{ Public voidAccept (Execute exe) {exe.method ( This); }}classSon2extendsfather{ Public voidAccept (Execute exe) {exe.method ( This); }} classExecute { Public voidmethod (Father Father) {System.out.println ("This is Father ' s method"); } Public voidmethod (Son1 son) {System.out.println ("This is Son1 ' s method"); } Public voidmethod (Son2 son) {System.out.println ("This is Son2 ' s method"); }} Public classTest { Public Static voidMain (string[] args) {Father Father=NewFather (); Father S1=NewSon1 (); Father S2=NewSon2 (); Execute EXE=NewExecute (); Father.accept (EXE); S1.accept (EXE); S2.accept (EXE); }}
Java single-pair mechanism understanding