-
為什么用適配器?查看全部
-
適配器模式兩種方式,組合和繼承查看全部
-
什么是適配器模式?查看全部
-
作用:1.透明 2.重用 3.低耦合查看全部
-
package com.imooc.pattern.adapter; /* * 采用繼承方式的插座適配器 */ public class TwoPlugAdapterExtends extends GBTwoPlug implements ThreePlugIf { @Override public void powerWithThree() { System.out.print("借助繼承適配器"); this.powerWithTwo(); } }查看全部
-
package com.imooc.pattern.adapter; public class NoteBook { private ThreePlugIf plug; public NoteBook(ThreePlugIf plug){ this.plug = plug; } // 使用插座充電 public void charge(){ plug.powerWithThree(); } public static void main(String[] args) { GBTwoPlug two = new GBTwoPlug(); ThreePlugIf three = new TwoPlugAdapter(two); NoteBook nb = new NoteBook(three); nb.charge(); three = new TwoPlugAdapterExtends(); nb = new NoteBook(three); nb.charge(); } } 運(yùn)行結(jié)果: 通過轉(zhuǎn)化 使用二相電流供電 借助繼承適配器使用二相電流供電查看全部
-
適配器分類: 1.組合 2.繼承查看全部
-
插座適配器分析查看全部
-
package com.imooc.pattern.adapter; /* * 二相轉(zhuǎn)三相插座的適配器 */ public class TwoPlugAdapter implements ThreePlugIf { private GBTwoPlug plug; public TwoPlugAdapter(GBTwoPlug plug){ this.plug = plug; } @Override public void powerWithThree() { System.out.println("通過轉(zhuǎn)化"); plug.powerWithTwo(); } }查看全部
-
package com.imooc.pattern.adapter; public class NoteBook { private ThreePlugIf plug; public NoteBook(ThreePlugIf plug){ this.plug = plug; } // 使用插座充電 public void charge(){ plug.powerWithThree(); } public static void main(String[] args) { GBTwoPlug two = new GBTwoPlug(); ThreePlugIf three = new TwoPlugAdapter(two); NoteBook nb = new NoteBook(three); nb.charge(); } } 運(yùn)行結(jié)果: 通過轉(zhuǎn)化 使用二相電流供電查看全部
-
package com.imooc.pattern.adapter; public class GBTwoPlug { // 使用二相電流供電 public void powerWithTwo(){ System.out.println("使用二相電流供電"); } }查看全部
-
package com.imooc.pattern.adapter; /* * 三相插座接口 */ public interface ThreePlugIf { // 使用三相電流供電 public void powerWithThree(); }查看全部
-
適配器查看全部
-
組合模式類圖查看全部
-
適配器模式 構(gòu)成:客戶端、目標(biāo)接口、原本接口、具體請(qǐng)求查看全部
舉報(bào)
0/150
提交
取消