设计模式(23)——访问者模式与双分派

来源:互联网 发布:重度抑郁症的表现知乎 编辑:程序博客网 时间:2024/05/22 13:21

一、java的动态绑定

所谓的动态绑定就是指程执行期间(而不是在编译期间)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法。java继承体系中的覆盖就是动态绑定的,看一下如下的代码:

[java] view plaincopyprint?
  1. class Father {  
  2.     public void method() {  
  3.         System.out.println("This is Father's method");  
  4.     }  
  5. }  
  6.   
  7. class Son1 extends Father {  
  8.     public void method() {  
  9.         System.out.println("This is Son1's method");  
  10.     }  
  11. }  
  12.   
  13. class Son2 extends Father {  
  14.     public void method() {  
  15.         System.out.println("This is Son2's method");  
  16.     }  
  17. }  
  18.   
  19. public class Test {  
  20.     public static void main(String[] args) {  
  21.         Father s1 = new Son1();  
  22.         s1.method();  
  23.   
  24.         Father s2 = new Son2();  
  25.         s2.method();  
  26.     }  
  27. }  

运行结果如下:

This is Son1's method

This is Son2's method

通过运行结果可以看到,尽管我们引用的类型是Father类型的,但是运行时却是调用的它实际类型(也就是Son1和Son2)的方法,这就是动态绑定。在java语言中,继承中的覆盖就是是动态绑定的,当我们用父类引用实例化子类时,会根据引用的实际类型调用相应的方法。

二、java的静态绑定

相对于动态绑定,静态绑定就是指在编译期就已经确定执行哪一个方法。在java中,方法的重载(方法名相同而参数不同)就是静态绑定的,重载时,执行哪一个方法在编译期就已经确定下来了。看一下代码:

[java] view plaincopyprint?
  1. class Father {  
  2. }  
  3.   
  4. class Son1 extends Father {  
  5. }  
  6.   
  7. class Son2 extends Father {  
  8. }  
  9.   
  10. class Execute {  
  11.     public void method(Father father) {  
  12.         System.out.println("This is Father's method");  
  13.     }  
  14.   
  15.     public void method(Son1 son) {  
  16.         System.out.println("This is Son1's method");  
  17.     }  
  18.   
  19.     public void method(Son2 son) {  
  20.         System.out.println("This is Son2's method");  
  21.     }  
  22. }  
  23.   
  24. public class Test {  
  25.     public static void main(String[] args) {  
  26.         Father father = new Father();  
  27.         Father s1 = new Son1();  
  28.         Father s2 = new Son2();  
  29.   
  30.         Execute exe = new Execute();  
  31.         exe.method(father);  
  32.         exe.method(s1);  
  33.         exe.method(s2);  
  34.     }  
  35. }  

运行结果如下:

This is Father's method

This is Father's method

This is Father's method

在这里,程序在编译的时候就已经确定使用method(Father father)方法了,不管我们在运行的时候传入的实际类型是什么,它永远都只会执行method(Father                           father)这个方法。也就是说,java的重载是静态绑定的。

三、instanceof操作符与转型

有时候,我们希望在使用重载的时候,程序能够根据传入参数的实际类型动态地调用相应的方法,也就是说,我们希望java的重载是动态的,而不是静态的。但是由于java的重载不是动态绑定,我们只能通过程序来人为的判断,我们一般会使用instanceof操作符来进行类型的判断。我们要对method(Father father)进行修改,在方法体中判断运行期间的实际类型,修改后的method(Father father)方法如下:

[java] view plaincopyprint?
  1. public void method(Father 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. }  

请注意,我们必须把判断是否是父类的条件(也就是判断是否为Father类的条件)放到最后,否则将一律会被判断为Father类,达不到我们动态判断的目的。修改代码后,程序就可以动态地根据参数的实际类型来调用相应的方法了。运行结果如下

This is Father's method

This is Son1's method

This is Son2's method

但是这种实现方式有一个明显的缺点,它是伪动态的,仍然需要我们来通过程序来判断类型。假如Father有100个子类的话,还是这样来实现显然是不合适的。必须通过其他更好的方式实现才行,我们可以使用双分派方式来实现动态绑定。

四、用双分派实现动态绑定

首先,什么是双分派?还记得23种设计模式(9):访问者模式中一开始举的例子吗?

类A中的方法method1和method2的区别就是,method2是双分派。我们可以看一下java双分派的特点:首先要有一个访问类B,类B提供一个showA(A a) 方法,在方法中,调用类A的method1方法,然后类A的method2方法中调用类B的showA方法并将自己作为参数传给showA。双分派的核心就是这个this对象。说到这里,我们已经明白双分派是怎么回事了,但是它有什么效果呢?就是可以实现方法的动态绑定,我们可以对上面的程序进行修改,代码如下:

[java] view plaincopyprint?
  1. class Father {  
  2.     public void accept(Visitor visitor) {  
  3.         visitor.method(this);  
  4.     }  
  5. }  
  6.   
  7. class Son1 extends Father {  
  8.     public void accept(Visitor visitor) {  
  9.         visitor.method(this);  
  10.     }  
  11. }  
  12.   
  13. class Son2 extends Father {  
  14.     public void accept(Visitor visitor) {  
  15.         visitor.method(this);  
  16.     }  
  17. }  
  18.   
  19. class Visitor {  
  20.     public void method(Father father) {  
  21.         System.out.println("This is Father's method");  
  22.     }  
  23.   
  24.     public void method(Son1 son) {  
  25.         System.out.println("This is Son1's method");  
  26.     }  
  27.   
  28.     public void method(Son2 son) {  
  29.         System.out.println("This is Son2's method");  
  30.     }  
  31. }  
  32.   
  33. public class Test {  
  34.     public static void main(String[] args) {  
  35.         Father father = new Father();  
  36.         Father s1 = new Son1();  
  37.         Father s2 = new Son2();  
  38.   
  39.         Visitor visitor = new Visitor();  
  40.         father.accept(visitor);  
  41.         s1.accept(visitor);  
  42.         s2.accept(visitor);  
  43.     }  
  44. }  

可以看到我们修改的地方,在Father,Son1,Son2中分别加入一个双分派的方法。调用的时候,原本是调用Visitor的method方法,现在改为调用Father的accept方法。运行结果如下:

This is Father's method

This is Son1's method

This is Son2's method

运行结果符合我们的预期,实现了动态绑定。双分派实现动态绑定的本质,就是在重载方法委派的前面加上了继承体系中覆盖的环节,由于覆盖是动态的,所以重载就是动态的了,与使用instanceof操作符的效果是一样的(用instanceof操作符可以实现重载方法动态绑定的原因也是因为instanceof操作符是动态的)。但是与使用instanceof操作符实现动态绑定相比,双分派方式的可扩展性要好的多。

0 0
原创粉丝点击