赞
踩
适配器模式(Adapter Pattern)是一种结构型设计模式,它将一个类的接口转换成客户端所期望的另一种接口,让原本不兼容的接口可以在一起工作。适配器模式常被用于将旧的代码和新的代码无缝地集成在一起,从而减少系统重构的成本。
在软件设计当中,系统的数据和行为都正确, 但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。
适配器模式的实现分为类适配器和对象适配器两种方式。
对象适配器结构图
Target(这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口)
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:22 * 客户端期待接口 */ public class Target { public void request() { System.out.println("客户普通请求!!"); } }
Adaptee(需要适配的类)
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:23 * 需要适配的类 */ public class Adaptee { public void specificRequest(){ System.out.println("需要适配的特殊请求"); } }
Adapter(通过在内部包装一个Adaptee对象,把源接口转换成目标接口)
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:25 * 适配类-对接两个适配的接口 */ public class Adapter extends Target{ /** * Adaptee对象 */ private Adaptee adaptee = new Adaptee(); /** * 适配的请求 */ public void request(){ adaptee.specificRequest(); } }
客户端:
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:27 */ public class AdapterClient { public static void main(String[] args) { Target target = new Adapter(); // 客户端调用的就是Target的request方法 target.request(); } }
输出的就是:需要适配的请求
例子:最开始姚明在NBA打篮球,他不懂英文,需要翻译,这就很难懂队友的意思,所以说要使用适配器来将外国队友和姚明之间进行适配,具体实现如下。
程序结构图
后卫、中锋、前锋都是球员,所以应该有一个球员抽象类,有进攻和防守的方法。
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:42 * 球员 */ public abstract class Player { protected String name; public Player(String name) { this.name = name; } // 进攻 public abstract void attack(); // 防守 public abstract void defense(); }
前锋
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:43 * 前锋 */ public class Forwards extends Player { public Forwards(String name) { super(name); } @Override public void attack() { System.out.println("前锋:" + this.name + "进攻"); } @Override public void defense() { System.out.println("前锋:" + this.name + "防守"); } }
中锋:姚明因为要翻译才能听懂队友的信号
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:50 * 适配器 - 翻译类 将姚明和队友之间的沟通进行互通 */ public class Translator extends Player{ private ForeignCenter foreignCenter = new ForeignCenter(); public Translator(String name) { super(name); foreignCenter.setName(name); } @Override public void attack() { foreignCenter.进攻(); } @Override public void defense() { foreignCenter.防守(); } }
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:43 * 外籍中锋 - 姚明 */ public class ForeignCenter { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void 进攻() { System.out.println("中锋:" + this.name + "进攻"); } public void 防守() { System.out.println("中锋:" + this.name + "防守"); } }
这里使用中文作为方法名,虽然可以进行运行,但是不建议这样使用,只是为了更好的说明适配器模式
后卫类
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:43 * 后卫 */ public class Guards extends Player { public Guards(String name) { super(name); } @Override public void attack() { System.out.println("后卫:" + this.name + "进攻"); } @Override public void defense() { System.out.println("后卫:" + this.name + "防守"); } }
客户端类:
java
复制代码
/** * @author Shier * CreateTime 2023/5/10 16:46 */ public class AdapterClient { public static void main(String[] args) { Player forwards = new Forwards("巴蒂尔"); forwards.attack(); Player guards = new Guards("麦格雷迪"); guards.attack(); Player center = new Translator("姚明"); center.defense(); center.attack(); } }
输出结果:
尽管姚明曾经是不太懂英文,尽管火箭教练和球员也不会学 中文,但因为有了翻译者,团队沟通合作成为可能,
适配器模式的优点
适配器模式的主要缺点
适配器使用场景:
作者:十二Y
链接:https://juejin.cn/post/7232600368751083578
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。