適配器模式
適配器模式可以把兩個不相關的類組裝在一起使用。實際上是繼承和組合的綜合運用。
讓我們來看一個例子:
public interface Itable{
public void say(String str);
}
public interface Ichair{
public void say(String str);
}
public class Table implements? Itable{
public void say(String str){
System.out.println(" i am a table :"+str);
}
}
public class Chair implements Ichair{
public void say(String str){
System.out.println(" i am a chair :"+str);
}
}
public class AdapterFitment? extends Table{
private Ichair c;
? public AdapterFitment(Ichair c){
? this.c=c;
}
public void insert(String str){c.say(str);}
}
然后呢,這個組合家具就即能當桌子用又能當椅子用了。