网站建设个人接单,手机版网站优化,淄博张店外贸建站公司,wordpress add term目录 一、适配器模式
一句话概括结构式模式
1.1、适配器模式概述
1.2、案例
1.2.1、类适配器模式实现案例
1.2.2、对象适配器
1.2.3、接口适配器
1.3、优缺点#xff08;对象适配器模式#xff09;
1.4、应用场景 一、适配器模式 一句话概括结构式模式
教你将类和对…目录 一、适配器模式
一句话概括结构式模式
1.1、适配器模式概述
1.2、案例
1.2.1、类适配器模式实现案例
1.2.2、对象适配器
1.2.3、接口适配器
1.3、优缺点对象适配器模式
1.4、应用场景 一、适配器模式 一句话概括结构式模式
教你将类和对象结合再一起形成一个更强大的结构. 1.1、适配器模式概述
将一个类的接口转换成客户希望的另一个接口使得原本不兼容的接口能一起工作.
比如如果你使用的是苹果手机那么就意味着充电器的充电口也是苹果标准的而你现在只有一个 type-c 插孔的插座能充电因此就需要一个转换器一头type-c另一头 苹果插头就可以让原本不兼容的 苹果插头 一起工作.
适配器模式包含以下主机角色
目标接口当前客户所期待的接口它可以是抽象类或者接口上述的 苹果插头.适配者类是被访问的现存组件库中的接口上述的 type-c.适配器类是一个转换器通过继承或引用目标接口实现适配者类的所有方法就可以实现转换效果.
适配器模式分为 类适配器模式、对象适配器模式 其中类适配器耦合度最高不符合合成/聚合复用原则且要求程序员了解现有组件库的内部结构因此应用较少.
还有一种模式叫 接口适配器模式是对对象适配器的扩展. 1.2、案例
现有一台电脑只能读取 SD 卡而我现在只有一个 TF 卡因此就需要使用适配器模式. 创建一个读卡器将 TF 卡中的内容读取出来. 1.2.1、类适配器模式实现案例
类适配器只需要我们继承目标接口实现适配者接口的所有方法即可.
/*** 目标接口: TF 卡*/
public interface TFCard {/*** 读取 TF 卡* return*/String readTF();/*** 写入 TF 卡*/void writeTF(String msg);}/*** 目标接口实现类*/
public class TFCardImpl implements TFCard{Overridepublic String readTF() {String msg tf card readTF: hello!;return msg;}Overridepublic void writeTF(String msg) {System.out.println(tf card writeTF: hello!);}}/*** 适配者接口: SD 卡*/
public interface SDCard {/*** 读取 SD 卡* return*/String readSD();/*** 写入 SD 卡*/void writeSD(String msg);}/*** 适配者实现类: SD 卡实现类*/
public class SDCardImpl implements SDCard {Overridepublic String readSD() {String msg sd card readTF: hello!;return msg;}Overridepublic void writeSD(String msg) {System.out.println(sd card writeTF: msg);}}/*** 适配器SD 兼容 TF*/
public class SDAdapterTF extends TFCardImpl implements SDCard{Overridepublic String readSD() {System.out.println(adapter read tf card);return readTF();}Overridepublic void writeSD(String msg) {System.out.println(adapter write tf card);writeTF(msg);}}/*** 电脑类*/
public class Computer {public String readSD(SDCard sdCard) {if(sdCard null) {throw new NullPointerException(sd card null);}return sdCard.readSD();}}public static void main(String[] args) {//1.创建一个电脑类Computer computer new Computer();//3.通过适配器从电脑中读取 TF 卡的数据SDAdapterTF adapter new SDAdapterTF();String msg computer.readSD(adapter);System.out.println(msg);}1.2.2、对象适配器
对象适配器相比于 类适配器更符合 合成/聚合复用原则持有新对象的引用而不是通过继承来达到复用目的. 也就是说它是通过持有目标接口的引用tf 卡接口的引用重写 适配者接口 的所有方法实现的 .
/*** 目标接口: TF 卡*/
public interface TFCard {/*** 读取 TF 卡* return*/String readTF();/*** 写入 TF 卡*/void writeTF(String msg);}/*** 目标接口实现类*/
public class TFCardImpl implements TFCard {Overridepublic String readTF() {String msg tf card readTF: hello!;return msg;}Overridepublic void writeTF(String msg) {System.out.println(tf card writeTF: hello!);}}/*** 适配者接口: SD 卡*/
public interface SDCard {/*** 读取 SD 卡* return*/String readSD();/*** 写入 SD 卡*/void writeSD(String msg);}/*** 适配者实现类: SD 卡实现类*/
public class SDCardImpl implements SDCard {Overridepublic String readSD() {String msg sd card readTF: hello!;return msg;}Overridepublic void writeSD(String msg) {System.out.println(sd card writeTF: msg);}}/*** 适配器SD 兼容 TF*/
public class SDAdapterTF implements SDCard {private TFCard tfCard;public SDAdapterTF(TFCard tfCard) {this.tfCard tfCard;}Overridepublic String readSD() {System.out.println(adapter read tf card);return tfCard.readTF();}Overridepublic void writeSD(String msg) {System.out.println(adapter write tf card);tfCard.writeTF(msg);}}/*** 电脑类*/
public class Computer {public String readSD(SDCard sdCard) {if(sdCard null) {throw new NullPointerException(sd card null);}return sdCard.readSD();}}public class Client {public static void main(String[] args) {//1.创建一个电脑类Computer computer new Computer();//3.通过适配器从电脑中读取 TF 卡的数据SDAdapterTF adapter new SDAdapterTF(new TFCardImpl());computer.readSD(adapter);}}1.2.3、接口适配器
当我们不希望实现一个适配者接口sd 卡接口中的所有方法时可以创建一个抽象类 Adapter实现所有方法不用实现方法内容.此时我们只需要继承该抽象类在重写我们需要的方法即可.
实现前两个适配器中就一直没有使用 writeSD 方法因此这里就不实现此方法.
/*** 目标接口: TF 卡*/
public interface TFCard {/*** 读取 TF 卡* return*/String readTF();/*** 写入 TF 卡*/void writeTF(String msg);}/*** 目标接口实现类*/
public class TFCardImpl implements TFCard {Overridepublic String readTF() {String msg tf card readTF: hello!;return msg;}Overridepublic void writeTF(String msg) {System.out.println(tf card writeTF: hello!);}}/*** 适配者接口: SD 卡*/
public interface SDCard {/*** 读取 SD 卡* return*/String readSD();/*** 写入 SD 卡*/void writeSD(String msg);}/*** 适配者实现类: SD 卡实现类*/
public class SDCardImpl implements SDCard {Overridepublic String readSD() {String msg sd card readTF: hello!;return msg;}Overridepublic void writeSD(String msg) {System.out.println(sd card writeTF: msg);}}public abstract class Adapter implements SDCard {Overridepublic void writeSD(String msg) {}Overridepublic String readSD() {return null;}}public class SDAdapterTF extends Adapter implements SDCard{private TFCard tfCard;public SDAdapterTF(TFCard tfCard) {this.tfCard tfCard;}Overridepublic String readSD() {System.out.println(adapter read tf card);return tfCard.readTF();}
}/*** 电脑类*/
public class Computer {public String readSD(SDCard sdCard) {if(sdCard null) {throw new NullPointerException(sd card null);}return sdCard.readSD();}}public class Client {public static void main(String[] args) {//1.创建一个电脑类Computer computer new Computer();//2.通过适配器从电脑中读取 TF 卡的数据SDAdapterTF sdAdapterTF new SDAdapterTF(new TFCardImpl());String msg computer.readSD(sdAdapterTF);System.out.println(msg);}}1.3、优缺点对象适配器模式 优点 1. 适配现有类且不修改类在不改变现有类的基础上实现现有类和目标类的接口的匹配.
2. 符合 合成/聚合 复用原则持有引用而不继承.
3. 符合开闭原则如果引入新的目标接口只需要在适配器类中进行扩展不需要修改原代码. 缺点 增加复杂性编写适配器类时要考虑全面包括适配者和目标类. 1.4、应用场景
1. 以前开发的系统中存在满足当前业务所需要的类但是接口和当前业务所需接口不一致.
2. 第三方提供的组件但是组件接口定义和自己要求的接口定义不同.