鍍金池/ 教程/ Java/ 訪問者模式討論篇:java的動態(tài)綁定與雙分派
訪問者模式
訪問者模式討論篇:java的動態(tài)綁定與雙分派
責(zé)任連模式
迭代器模式
策略模式
命令模式
單例模式
建造者模式
解釋器模式
工廠方法模式
備忘錄模式
原型模式
單例模式討論篇:單例模式與垃圾回收
觀察者模式
模版方法模式
創(chuàng)建類模式總結(jié)篇
抽象工廠模式
中介者模式

訪問者模式討論篇:java的動態(tài)綁定與雙分派

java的動態(tài)綁定

所謂的動態(tài)綁定就是指程執(zhí)行期間(而不是在編譯期間)判斷所引用對象的實際類型,根據(jù)其實際的類型調(diào)用其相應(yīng)的方法。java繼承體系中的覆蓋就是動態(tài)綁定的,看一下如下的代碼:

    class Father {
        public void method(){
            System.out.println("This is Father's method");
        }
    }

    class Son1 extends Father{
        public void method(){
            System.out.println("This is Son1's method");
        }
    }

    class Son2 extends Father{
        public void method(){
            System.out.println("This is Son2's method");
        }
    }

    public class Test {
        public static void main(String[] args){
            Father s1 = new Son1();
            s1.method();

            Father s2 = new Son2();
            s2.method();
        }
    }

運行結(jié)果如下:

This is Son1's method
This is Son2's method

通過運行結(jié)果可以看到,盡管我們引用的類型是Father類型的,但是運行時卻是調(diào)用的它實際類型(也就是Son1和Son2)的方法,這就是動態(tài)綁定。在java語言中,繼承中的覆蓋就是是動態(tài)綁定的,當(dāng)我們用父類引用實例化子類時,會根據(jù)引用的實際類型調(diào)用相應(yīng)的方法。

java的靜態(tài)綁定

相對于動態(tài)綁定,靜態(tài)綁定就是指在編譯期就已經(jīng)確定執(zhí)行哪一個方法。在java中,方法的重載(方法名相同而參數(shù)不同)就是靜態(tài)綁定的,重載時,執(zhí)行哪一個方法在編譯期就已經(jīng)確定下來了??匆幌麓a:

    class Father {}
    class Son1 extends Father{}
    class Son2 extends Father{}

    class Execute {
        public void method(Father father){
            System.out.println("This is Father's method");
        }

        public void method(Son1 son){
            System.out.println("This is Son1's method");
        }

        public void method(Son2 son){
            System.out.println("This is Son2's method");
        }
    }

    public class Test {
        public static void main(String[] args){
            Father father = new Father();
            Father s1 = new Son1();
            Father s2 = new Son2();

            Execute exe = new Execute();
            exe.method(father);
            exe.method(s1);
            exe.method(s2);
        }
    }

運行結(jié)果如下:

This is Father's method
This is Father's method
This is Father's method

在這里,程序在編譯的時候就已經(jīng)確定使用method(Father father)方法了,不管我們在運行的時候傳入的實際類型是什么,它永遠都只會執(zhí)行method(Father father)這個方法。也就是說,java的重載是靜態(tài)綁定的。

instanceof操作符與轉(zhuǎn)型

有時候,我們希望在使用重載的時候,程序能夠根據(jù)傳入?yún)?shù)的實際類型動態(tài)地調(diào)用相應(yīng)的方法,也就是說,我們希望java的重載是動態(tài)的,而不是靜態(tài)的。但是由于java的重載不是動態(tài)綁定,我們只能通過程序來人為的判斷,我們一般會使用instanceof操作符來進行類型的判斷。我們要對method(Father father)進行修改,在方法體中判斷運行期間的實際類型,修改后的method(Father father)方法如下:

    public void method(Father father){
        if(father instanceof Son1){
            method((Son1)father);
        }else if(father instanceof Son2){
            method((Son2)father);
        }else if(father instanceof Father){
            System.out.println("This is Father's method");
        }
    }

請注意,我們必須把判斷是否是父類的條件(也就是判斷是否為Father類的條件)放到最后,否則將一律會被判斷為Father類,達不到我們動態(tài)判斷的目的。修改代碼后,程序就可以動態(tài)地根據(jù)參數(shù)的實際類型來調(diào)用相應(yīng)的方法了。運行結(jié)果如下:

This is Father's method
This is Son1's method
This is Son2's method

但是這種實現(xiàn)方式有一個明顯的缺點,它是偽動態(tài)的,仍然需要我們來通過程序來判斷類型。假如Father有100個子類的話,還是這樣來實現(xiàn)顯然是不合適的。必須通過其他更好的方式實現(xiàn)才行,我們可以使用雙分派方式來實現(xiàn)動態(tài)綁定。

用雙分派實現(xiàn)動態(tài)綁定

首先,什么是雙分派?還記得23種設(shè)計模式(9):訪問者模式中一開始舉的例子嗎?

類A中的方法method1和method2的區(qū)別就是,method2是雙分派。我們可以看一下java雙分派的特點:首先要有一個訪問類B,類B提供一個showA(A a) 方法,在方法中,調(diào)用類A的method1方法,然后類A的method2方法中調(diào)用類B的showA方法并將自己作為參數(shù)傳給showA。雙分派的核心就是這個this對象。說到這里,我們已經(jīng)明白雙分派是怎么回事了,但是它有什么效果呢?就是可以實現(xiàn)方法的動態(tài)綁定,我們可以對上面的程序進行修改,代碼如下:

    class Father {
        public void accept(Execute exe){
            exe.method(this);
        }
    }
    class Son1 extends Father{
        public void accept(Execute exe){
            exe.method(this);
        }
    }
    class Son2 extends Father{
        public void accept(Execute exe){
            exe.method(this);
        }
    }

    class Execute {
        public void method(Father father){
            System.out.println("This is Father's method");
        }

        public void method(Son1 son){
            System.out.println("This is Son1's method");
        }

        public void method(Son2 son){
            System.out.println("This is Son2's method");
        }
    }

    public class Test {
        public static void main(String[] args){
            Father father = new Father();
            Father s1 = new Son1();
            Father s2 = new Son2();

            Execute exe = new Execute();
            father.accept(exe);
            s1.accept(exe);
            s2.accept(exe);
        }
    }

可以看到我們修改的地方,在Father,Son1,Son2中分別加入一個雙分派的方法。調(diào)用的時候,原本是調(diào)用Execute的method方法,現(xiàn)在改為調(diào)用Father的accept方法。運行結(jié)果如下:

This is Father's method
This is Son1's method
This is Son2's method

運行結(jié)果符合我們的預(yù)期,實現(xiàn)了動態(tài)綁定。雙分派實現(xiàn)動態(tài)綁定的本質(zhì),就是在重載方法委派的前面加上了繼承體系中覆蓋的環(huán)節(jié),由于覆蓋是動態(tài)的,所以重載就是動態(tài)的了,與使用instanceof操作符的效果是一樣的(用instanceof操作符可以實現(xiàn)重載方法動態(tài)綁定的原因也是因為instanceof操作符是動態(tài)的)。但是與使用instanceof操作符實現(xiàn)動態(tài)綁定相比,雙分派方式的可擴展性要好的多。

上一篇:策略模式下一篇:責(zé)任連模式