适配器模式
- 主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。
- 应用实例: 1、美国电器 110V,中国 220V,就要有一个适配器将 110V 转化为 220V。 2、JAVA JDK 1.1 提供了 Enumeration 接口,而在 1.2 中提供了 Iterator 接口,想要使用 1.2 的 JDK,则要将以前系统的 Enumeration 接口转化为 Iterator 接口,这时就需要适配器模式。 3、在 LINUX 上运行 WINDOWS 程序。 4、JAVA 中的 jdbc。
下面以电脑读取SD卡为例
- 创建SD卡接口
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午4:43 2019/8/7 */ public interface SDCard { /** * 读取SD卡方法 * * @return */ String readSD(); /** * 写入SD卡功能 * @param msg * @return */ int writeSD(String msg); }
- 创建SD卡实现类
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午4:45 2019/8/7 */ public class SDCardImpl implements SDCard { @Override public String readSD() { String msg="sdcard read a msg : Hello word sd"; return msg; } @Override public int writeSD(String msg) { System.out.println("sd card write msg:" + msg); return 1; } }
- 创建TFCard接口
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午5:12 2019/8/7 */ public interface TFCard { String readTF(); int writeTF(String msg); }
- 创建TFCard实现类
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午5:15 2019/8/7 */ public class TFCardImpl implements TFCard { @Override public String readTF() { String msg ="tf card reade msg : hello word tf card"; return msg; } @Override public int writeTF(String msg) { System.out.println("tf card write a msg : " + msg); return 1; } }
- 创建SDAdapterTF适配器(实现SD卡的接口,但最终实现是调用TFCard的接口)
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 上午10:45 2019/8/8 */ public class SDAdapterTF implements SDCard { private TFCard tfCard; public SDAdapterTF(TFCard tfCard) { this.tfCard = tfCard; } /** * 读取TF卡也走SDCard的readSD方法,但是具体实现仍然走的是TFCard的readTF方法 * @return */ @Override public String readSD() { System.out.println("adapter read tf card"); return tfCard.readTF(); } @Override public int writeSD(String msg) { System.out.println("adapter write tf card"); return tfCard.writeTF(msg); } }
- 创建Computer接口
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午4:52 2019/8/7 */ public interface Computer { /** * 读取sd卡 * @param sdCard * @return */ String readSD(SDCard sdCard); }
- 创建ThinkpadComputer并实现Computer接口
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午4:53 2019/8/7 */ public class ThinkpadComputer implements Computer { @Override public String readSD(SDCard sdCard) { if(sdCard == null){ throw new NullPointerException("sd card null"); } return sdCard.readSD(); } }
- 创建Demo类
package com.design.demo.adapter; /** * @author: GuanBin * @date: Created in 下午5:04 2019/8/7 */ public class ComputerReadDemo { public static void main(String[] args) { Computer computer = new ThinkpadComputer(); SDCardImpl sdCard = new SDCardImpl(); System.out.println(computer.readSD(sdCard)); System.out.println("========================="); TFCardImpl tfCard = new TFCardImpl(); SDAdapterTF sdAdapterTF = new SDAdapterTF(tfCard); System.out.println(computer.readSD(sdAdapterTF)); } }