標籤:
適配器模式定義:將兩個不相容的類糾合在一起使用,屬於結構型模式,需要有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方法。
適配器模式
概述
將一個類的介面轉換成客戶希望的另外一個介面。Adapter模式使得原本由於介面不相容而不能一起工作的那些類可以一起工作。
適用性
1.你想使用一個已經存在的類,而它的介面不符合你的需求。 2.你想建立一個可以複用的類,該類可以與其他不相關的類或不可預見的類(即那些介面 可能不一定相容的類)協同工作。 3.(僅適用於對象Adapter)你想使用一些已經存在的子類,但是不可能對每一個都進行 子類化以匹配它們的介面。對象適配器可以適配它的父類介面。
參與者
1.Target 定義Client使用的與特定領域相關的介面。 2.Client 與符合Target介面的對象協同。 3.Adaptee 定義一個已經存在的介面,這個介面需要適配。 4.Adapter 對Adaptee的介面與Target介面進行適配
類圖
例子
Target
public interface Target { void adapteeMethod(); void adapterMethod();}
Adaptee
public class Adaptee { public void adapteeMethod() { System.out.println("Adaptee method!"); }}
Adapter
public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; }public void adapteeMethod() {adaptee.adapteeMethod();}public void adapterMethod() {System.out.println("Adapter method!"); }}
Client
public class Test { public static void main(String[] args) { Target target = new Adapter(new Adaptee()); target.adapteeMethod(); target.adapterMethod(); }}
result
Adaptee method!Adapter method!
Java適配器模式(Adapter模式)