23种设计模式之适配器模式
适配器模式
泰国旅游使用插座问题:
基本介绍
-
适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)
-
适配器模式属于结构型模式
适配器模式工作原理
-
适配器模式:将一个类的接口转换成另一种接口.让原本接口不兼容的类可以兼 容
-
从用户的角度看不到被适配者,是解耦的
-
用户调用适配器转化出来的目标接口方法,适配器再调用被适配者的相关接口
方法
-
用户收到反馈结果,感觉只是和目标接口交互,如图
类适配器模式介绍
类适配器应用实例

package com.atguigu.adapter.classadapter; //被适配的类 public class Voltage220V { //输出220V的电压 public int output220V() { int src = 220; System.out.println("电压=" + src + "伏"); return src; } }
IVoltage5V接口:
package com.atguigu.adapter.classadapter; //适配接口 public interface IVoltage5V { public int output5V(); }
VoltageAdapter类:
package com.atguigu.adapter.classadapter; //适配器类 public class VoltageAdapter extends Voltage220V implements IVoltage5V { @Override public int output5V() { // TODO Auto-generated method stub //获取到220V电压 int srcV = output220V(); int dstV = srcV / 44 ; //转成 5v return dstV; } }
Phone类:
package com.atguigu.adapter.classadapter; public class Phone { //充电 public void charging(IVoltage5V iVoltage5V) { if(iVoltage5V.output5V() == 5) { System.out.println("电压为5V, 可以充电~~"); } else if (iVoltage5V.output5V() > 5) { System.out.println("电压大于5V, 不能充电~~"); } } }
Client类:
package com.atguigu.adapter.classadapter; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(" === 类适配器模式 ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter()); } }
类适配器模式注意事项和细节
-
Java是单继承机制,所以类适配器需要继承src类这一点算是一个缺点, 因为这要求dst必须是接口,有一定局限性;
-
src类的方法在Adapter中都会暴露出来,也增加了使用的成本。
对象适配器模式介绍
-
基本思路和类的适配器模式相同,只是将Adapter类作修改,不是继承src类,而是持有src类的实例,以解决兼容性的问题。 即:持有 src类,实现 dst 类接口,完成src->dst的适配
-
根据“合成复用原则”,在系统中尽量使用关联关系(聚合)来替代继承关系。
-
对象适配器模式是适配器模式常用的一种
对象适配器模式应用实例

3)代码演示
package com.atguigu.adapter.objectadapter; //适配器类 public class VoltageAdapter implements IVoltage5V { private Voltage220V voltage220V; // 关联关系-聚合 //通过构造器,传入一个 Voltage220V 实例 public VoltageAdapter(Voltage220V voltage220v) { this.voltage220V = voltage220v; } @Override public int output5V() { int dst = 0; if(null != voltage220V) { int src = voltage220V.output220V();//获取220V 电压 System.out.println("使用对象适配器,进行适配~~"); dst = src / 44; System.out.println("适配完成,输出的电压为=" + dst); } return dst; } }
Client类:
package com.atguigu.adapter.objectadapter; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(" === 对象适配器模式 ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Voltage220V())); } }
对象适配器模式注意事项和细节
-
对象适配器和类适配器其实算是同一种思想,只不过实现方式不同。根据合成复用原则,使用组合替代继承, 所以它解决了类适配器必须继承src的局限性问题,也不再要求dst必须是接口。
接口适配器模式介绍
-
-
当不需要全部实现接口提供的方法时,可先设计一个抽象类实现接口,并为该接口中每个方法提供一个默认实现(空方法),那么该抽象类的子类可有选择地覆盖父类的某些方法来实现需求
-
接口适配器模式应用实例
1) Android中的属性动画ValueAnimator类可以通addListener(AnimatorListener listener)方法添加监听器, 那么常规写法如下:
ValueAnimator valueAnimat or = ValueAnimator.ofInt(0,100); valueAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); valueAnimator.start();
ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { //xxxx具体实现 } }); valueAnimator.start();
3) AnimatorListenerAdapter类,就是一个 接口适配器,代码如右图:它空实现了
public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener Animator.AnimatorPauseListener { @Override //默认实现 public void onAnimationCancel(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationPause(Animator animation) { } @Override public void onAnimationResume(Animator animation) { } }
4) AnimatorListener是一个接口.
public static interface AnimatorListener { void onAnimationStart(Animator animation); void onAnimationEnd(Animator animation); void onAnimationCancel(Animator animation); void onAnimationRepeat(Animator animation); }
new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { //xxxx具体实现 } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)