設計模式-適配器模式

顧名思義,就是把本來兩個沒法拼接的東西,經過適配器連接起來。java

好比三角充電器和二孔排插,經過一個三+二孔轉換器就能進行充電了ide

 

public interface ChangeI {
    /**
     * 用二孔充電
     */
    void excute2();
}
public class ChongDianQi {
    public void excute3(){
        System.out.println("充電器開始進行三插孔充電");
    }
}
public class Changer implements ChangeI{
    private ChongDianQi chongDianQi;

    public Changer(ChongDianQi chongDianQi) {
        this.chongDianQi = chongDianQi;
    }

    @Override
    public void excute2() {
        chongDianQi.excute3();
        System.out.println("適配器----轉換成二孔");

    }
}
public class ChongDianQi {
    public void excute3(){
        System.out.println("充電器開始進行三插孔充電");
    }
}
public class Demo {
    public static void main(String[] args) {
        Changer changer=new Changer(new ChongDianQi());//充電器對象爲參數進行構建
        PaiCha paiCha = new PaiCha(changer);
        paiCha.excute2();
    }
}
相關文章
相關標籤/搜索