天天看點

設計模式之Adapter(擴充卡)

定義:

将兩個不相容的類糾合在一起使用,屬于結構型模式,需要有Adaptee(被适配者)和Adaptor(擴充卡)兩個身份.

為何使用?

我們經常碰到要将兩個沒有關系的類組合在一起使用,第一解決方案是:修改各自類的接口,但是如果我們沒有源代碼,或者,我們不願意為了一個應用而修改各自的接口。 怎麼辦?

使用Adapter,在這兩種接口之間建立一個混合接口(混血兒).

如何使用?

實作Adapter方式,其實"think in Java"的"類再生"一節中已經提到,有兩種方式:組合(composition)和繼承(inheritance).

假設我們要打樁,有兩種類:方形樁 圓形樁.

public class SquarePeg{

  public void insert(String str){

    System.out.println("SquarePeg insert():"+str);

  }

}

public class RoundPeg{

  public void insertIntohole(String msg){

    System.out.println("RoundPeg insertIntoHole():"+msg);

}

}

現在有一個應用,需要既打方形樁,又打圓形樁.那麼我們需要将這兩個沒有關系的類綜合應用.假設RoundPeg我們沒有源代碼,或源代碼我們不想修改,那麼我們使用Adapter來實作這個應用:

public class PegAdapter extends SquarePeg{

  private RoundPeg roundPeg;

  public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)

  public void insert(String str){ roundPeg.insertIntoHole(str);}

}

在上面代碼中,RoundPeg屬于Adaptee,是被适配者.PegAdapter是Adapter,将Adaptee(被适配者RoundPeg)和Target(目标SquarePeg)進行适配.實際上這是将組合方法(composition)和繼承(inheritance)方法綜合運用.

PegAdapter首先繼承SquarePeg,然後使用new的組合生成對象方式,生成RoundPeg的對象roundPeg,再重載父類insert()方法。從這裡,你也了解使用new生成對象和使用extends繼承生成對象的不同,前者無需對原來的類修改,甚至無需要知道其内部結構和源代碼.

如果你有些Java使用的經驗,已經發現,這種模式經常使用。

進一步使用

上面的PegAdapter是繼承了SquarePeg,如果我們需要兩邊繼承,即繼承SquarePeg 又繼承RoundPeg,因為Java中不允許多繼承,但是我們可以實作(implements)兩個接口(interface)

public interface IRoundPeg{

  public void insertIntoHole(String msg);

}

public interface ISquarePeg{

  public void insert(String str);

}

下面是新的RoundPeg 和SquarePeg, 除了實作接口這一差別,和上面的沒什麼差別。

public class SquarePeg implements ISquarePeg{

  public void insert(String str){

    System.out.println("SquarePeg insert():"+str);

  }

}

public class RoundPeg implements IRoundPeg{

  public void insertIntohole(String msg){

    System.out.println("RoundPeg insertIntoHole():"+msg);

  }

}

下面是新的PegAdapter,叫做two-way adapter:

public class PegAdapter implements IRoundPeg,ISquarePeg{

  private RoundPeg roundPeg;

  private SquarePeg squarePeg;

  // 構造方法

  public PegAdapter(RoundPeg peg){this.roundPeg=peg;}

  // 構造方法

  public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)

  public void insert(String str){ roundPeg.insertIntoHole(str);}

}

還有一種叫Pluggable Adapters,可以動态的擷取幾個adapters中一個。使用Reflection技術,可以動态的發現類中的Public方法。