java設計模式6:Adapter

結構模式有下面這些:適配器模式,缺省適配模式,合成模式,裝飾模式,代理模式,享元模式,門面模式,橋接模式等.

適配器模式有類的適配器模式和對象的適配器模式兩種不同的形式。如下圖所示,左邊是類的適配器模式,右邊是對象的適配器模式。

image

類的適配器模式把被適配的類的API轉換成爲目標類的API,其靜態結構圖如下圖所示:

image

在上圖中可以看出,Adaptee類並沒有sampleOperation2()方法,而客戶端則期待這個方法。爲使客戶端能夠使用Adaptee類,提供一箇中間環節,即類Adapter,把Adaptee的API與Target類的API銜接起來。Adapter與Adaptee是繼承關係,這決定了這個適配器模式是類的。

角色:
1、目標(Target):這就是所期待得到的接口。
2、源(Adaptee):現有需要適配的接口。
3、適配器(Adapter):適配器類是本模式的核心。適配器把源接口轉換成目標接口。

代碼:

image

package com.javapatterns.adapter.classAdapter;

public class Adaptee {
    public void sampleOperation1(){
        System.out.println("方法名       "+Thread.currentThread().getStackTrace()[1].getMethodName());
        System.out.println("類名    "+Thread.currentThread().getStackTrace()[1].getClassName());
        System.out.println("文件名   " + Thread.currentThread().getStackTrace()[1].getFileName());
        System.out.println("所在的行數 "+Thread.currentThread().getStackTrace()[1].getLineNumber());
    }
}

 

 

package com.javapatterns.adapter.classAdapter;

public interface Target {
    /**
     * Class Adaptee contains operation sampleOperation1.
     */
    void sampleOperation1();

    /**
     * Class Adaptee doesn't contain operation sampleOperation2.
     */
    void sampleOperation2();
}

 

package com.javapatterns.adapter.classAdapter;

public class Adapter extends Adaptee implements Target {
    /**
     * Class Adaptee doesn't contain operation sampleOperation2.
     */
    public void sampleOperation2(){
        // Write your code here
        sampleOperation1();
    }
}

 

 

/**
* 測試類適配器模式
*/
package com.javapatterns.adapter.classAdapter;

/**
* @author luhx
*
*/
public class client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Adapter a= new Adapter();
        a.sampleOperation2();
    }

}

 

 

對象的適配器模式

對象的適配器模式把被適配的類的API轉換成爲目標類的API,與類的適配器模式不同的是,對象的適配器模式不是使用繼承關係連接到Adaptee類,而是使用委派關係連接到Adaptee類。

image 

從圖中可以看出,Adaptee類並沒有sampleOperation2()方法,而客戶端則期待這個方法。爲使客戶端能夠使用Adaptee類,需要提供一個包裝(Wrapper)類Adapter。這個包裝類包裝了一個Adaptee的實例,從而此包裝類能夠把Adaptee的API與Target類的API銜接起來。Adapter 與Adaptee是委派關係,這決定了這個適配器模式是對象的。

源代碼結構圖:

image

package com.javapatterns.adapter.objectAdapter;

/**
* @author luhx
*
*/
public class client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Adapter a = new Adapter(new Adaptee());
        a.sampleOperation2();
    }

}

 

package com.javapatterns.adapter.objectAdapter;

public class Adapter implements Target {
public Adapter(Adaptee adaptee){
        super();
        this.adaptee = adaptee;
    }

    public void sampleOperation1(){

    }

    public void sampleOperation2(){
        // Write your code here
        adaptee.sampleOperation1();
    }

    /**
     * @uml.property  name="adaptee"
     * @uml.associationEnd  multiplicity="(1 1)"
     */
    private Adaptee adaptee;
}

 

 

package com.javapatterns.adapter.objectAdapter;

public class Adaptee {
    public void sampleOperation1(){
        System.out.println("方法名       "+Thread.currentThread().getStackTrace()[1].getMethodName());
        System.out.println("類名    "+Thread.currentThread().getStackTrace()[1].getClassName());
        System.out.println("文件名   " + Thread.currentThread().getStackTrace()[1].getFileName());
        System.out.println("所在的行數 "+Thread.currentThread().getStackTrace()[1].getLineNumber());

    }
}

 

 

package com.javapatterns.adapter.objectAdapter;

public interface Target {
    /**
     * Class Adaptee contains operation sampleOperation1.
     */
    void sampleOperation1();

    /**
     * Class Adaptee doesn't contain operation sampleOperation2.
     */
    void sampleOperation2();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章