-
適配器模式實(shí)現(xiàn)的兩種方法:1)組合,2)繼承查看全部
-
采用組合方式的適配器成為對(duì)象適配器。 采用繼承(&實(shí)現(xiàn))方式的成為類(lèi)適配器。查看全部
-
適配器模式將一個(gè)類(lèi)的接口,轉(zhuǎn)換成客戶(hù)希望的另外一個(gè)接口,使得原本由于接口不兼容而不能一起工作的那些類(lèi)可以在一起工作。查看全部
-
適配器模式分類(lèi)查看全部
-
適配器UML圖查看全部
-
適配器作用查看全部
-
類(lèi)繼承的方式實(shí)現(xiàn)查看全部
-
兩種實(shí)現(xiàn)適配器的方法查看全部
-
1.ThreePlugIf接口 /* * 三相插座接口 */ public interface ThreePlugIf { //使用三相電流供電 public void powerWithThree(); } 2.GBTwoPlug.java(GB兩廂插口) public class GBTwoPlug { //使用二相電流供電 public void powerWithTwo(){ System.out.println("使用二相電流供電"); } } 3.TwoPlugAdapter.java /* * 二相轉(zhuǎn)三相的插座適配器 */ public class TwoPlugAdapter implements ThreePlugIf { private GBTwoPlug plug; public TwoPlugAdapter(GBTwoPlug plug){ this.plug = plug; } @Override public void powerWithThree() { System.out.println("通過(guò)轉(zhuǎn)化"); plug.powerWithTwo(); } } 4.NoteBook.java 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(); } }查看全部
-
適配器模式的4部分組成查看全部
-
適配器的定義查看全部
-
適配器實(shí)現(xiàn)的兩種方式查看全部
-
繼承:類(lèi)適配器查看全部
-
對(duì)象適配器查看全部
-
適配器作用查看全部
舉報(bào)
0/150
提交
取消