Visitor Mode Discussion: dynamic binding and double dispatch of Java

Source: Internet
Author: User

Reproduced http://blog.csdn.net/zhengzhb/article/category/926691

Dynamic binding of Java

The so-called dynamic binding refers to determining the actual type of the referenced object during the execution period (rather than during the compilation period), and calling the corresponding method based on the actual type. The coverage in the Java inheritance system is dynamically bound. Take a look at the following code:

[Java]View plaincopy
  1. Class father {
  2. Public void method (){
  3. System. Out. println ("this is Father's method ");
  4. }
  5. }
  6. Class son1 extends father {
  7. Public void method (){
  8. System. Out. println ("this is son1's method ");
  9. }
  10. }
  11. Class son2 extends father {
  12. Public void method (){
  13. System. Out. println ("this is son2's method ");
  14. }
  15. }
  16. Public class test {
  17. Public static void main (string [] ARGs ){
  18. Father S1 = new son1 ();
  19. S1.method ();
  20. Father S2 = new son2 ();
  21. S2.method ();
  22. }
  23. }

The running result is as follows:

This is son1's Method
This is son2's Method

The running result shows that although the referenced type is of the father type, the actual type (namely, son1 and son2) method is called during the running, which is dynamic binding. In Java, override in inheritance is dynamically bound. When we use the parent class to reference the instantiated subclass, the corresponding method will be called based on the actual referenced type.

 

Static binding of Java

Compared with dynamic binding, static binding determines which method to execute during the compilation period. In Java, the method overload (with the same method name and different parameters) is statically bound. During the overload, the method to be executed is determined during the compilation period. Take a look at the Code:

[Java]View plaincopy
  1. Class father {}
  2. Class son1 extends father {}
  3. Class son2 extends father {}
  4. Class execute {
  5. Public void method (Father ){
  6. System. Out. println ("this is Father's method ");
  7. }
  8. Public void method (son1 son ){
  9. System. Out. println ("this is son1's method ");
  10. }
  11. Public void method (son2 son ){
  12. System. Out. println ("this is son2's method ");
  13. }
  14. }
  15. Public class test {
  16. Public static void main (string [] ARGs ){
  17. Father = new father ();
  18. Father S1 = new son1 ();
  19. Father S2 = new son2 ();
  20. Execute EXE = new execute ();
  21. EXE. Method (father );
  22. EXE. Method (S1 );
  23. EXE. Method (S2 );
  24. }
  25. }

The running result is as follows:

This is Father's Method
This is Father's Method
This is Father's Method

Here, the program has determined to use the method (Father) method during compilation, regardless of the actual type we pass in during runtime, it will always only execute the method (Father) method. That is to say, the Java overload is statically bound.

 

Instanceof operator and Transformation

Sometimes, we want the program to dynamically call the corresponding method based on the actual type of input parameters when using the overload, that is, we want the Java overload to be dynamic, not static. However, because the Java overload is not dynamic binding, we can only judge the type by the operator of the program. We generally use the instanceof operator to judge the type. We need to modify the method (Father) to determine the actual type during running in the method body. The modified method (Father) method is as follows:

[Java]View plaincopy
  1. Public void method (Father ){
  2. If (father instanceof son1 ){
  3. Method (son1) Father );
  4. } Else if (father instanceof son2 ){
  5. Method (son2) Father );
  6. } Else if (father instanceof father ){
  7. System. Out. println ("this is Father's method ");
  8. }
  9. }

Please note that we must put the condition for determining whether the class is a parent class (that is, the condition for determining whether the class is a father class) At the end, otherwise it will be regarded as a father class, we cannot achieve our goal of dynamic judgment. After the code is modified, the program can dynamically call the corresponding method based on the actual type of the parameter. The running result is as follows:

This is Father's Method
This is son1's Method
This is son2's Method

However, this implementation method has an obvious disadvantage. It is pseudo-dynamic, and we still need to judge the type through the program. If father has 100 sub-classes, it is obviously inappropriate to do so. It must be implemented in other better ways. We can use the double dispatch method to implement dynamic binding.

Dynamic binding using double dispatch

First, what is double dispatch? Remember23 design modes (9): Visitor ModeFirst example?

The difference between Methods Method1 and method2 in Class A is that method2 is a double dispatch. Let's take a look at the features of Java double DISPATCH: first, there must be a primary class B. Class B provides a Showa (A) method. In the method, call the Method1 method of Class, then, the method2 method of Class A calls the Showa method of class B and passes it as a parameter to showa. This object is the core of Dual-allocation. Speaking of this, we already know what the dual-assignment is, but what effect does it have? The method can be dynamically bound. We can modify the above program. The Code is as follows:

[Java]View plaincopy
  1. Class father {
  2. Public void accept (execute exe ){
  3. EXE. Method (this );
  4. }
  5. }
  6. Class son1 extends father {
  7. Public void accept (execute exe ){
  8. EXE. Method (this );
  9. }
  10. }
  11. Class son2 extends father {
  12. Public void accept (execute exe ){
  13. EXE. Method (this );
  14. }
  15. }
  16. Class execute {
  17. Public void method (Father ){
  18. System. Out. println ("this is Father's method ");
  19. }
  20. Public void method (son1 son ){
  21. System. Out. println ("this is son1's method ");
  22. }
  23. Public void method (son2 son ){
  24. System. Out. println ("this is son2's method ");
  25. }
  26. }
  27. Public class test {
  28. Public static void main (string [] ARGs ){
  29. Father = new father ();
  30. Father S1 = new son1 ();
  31. Father S2 = new son2 ();
  32. Execute EXE = new execute ();
  33. Father. Accept (exe );
  34. S1.accept (exe );
  35. S2.accept (exe );
  36. }
  37. }

We can see the modified location. Add a double dispatch method to father, son1, and son2 respectively. When calling, it was originally called the execute method. Now it is changed to calling the Father accept method. The running result is as follows:

This is Father's Method
This is son1's Method
This is son2's Method

The running result conforms to our expectation and implements dynamic binding. The essence of the dynamic binding of double dispatch is to add the inheritance system coverage before the delegation of the overload method. Because the coverage is dynamic, the overload is dynamic, the effect of using the instanceof operator is the same (the reason why the instanceof operator can realize dynamic binding of overload methods is also because the instanceof operator is dynamic ). However, compared to the dynamic binding using the instanceof operator, the scalability of the double dispatch method is much better.

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.