当前位置: 首页 > news >正文

设计模式之【适配器模式】

类适配器实现(继承)

类适配器通过继承来实现适配器功能

// 目标接口
public interface Target {void request();
}// 被适配者
public class Adaptee {public void specificRequest() {System.out.println("Adaptee: specificRequest");}
}// 适配器
public class Adapter extends Adaptee implements Target {/*** 采用继承的方式实现转换功能*/@Overridepublic void request() {super.specificRequest();}
}// 客户端代码
public class Client {public static void main(String[] args) {Adaptee adaptee = new Adaptee();Target target = new Adapter(adaptee);target.request(); // 通过适配器调用被适配者方法}
}

对象适配器实现(组合)

对象适配器通过组合来实现适配器功能

// 目标接口
public interface Target {void request();
}// 被适配者
public class Adaptee {public void specificRequest() {System.out.println("Adaptee: specificRequest");}
}// 适配器
public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee = adaptee;}@Overridepublic void request() {adaptee.specificRequest();}
}// 客户端代码
public class Client {public static void main(String[] args) {Adaptee adaptee = new Adaptee();Target target = new Adapter(adaptee);target.request(); // 通过适配器调用被适配者方法}
}
http://www.lryc.cn/news/384663.html

相关文章:

  • AI论文降重:一键操作,让你的论文查重率瞬间下降
  • Cmake--学习笔记
  • LangChain让LLM带上记忆
  • Word恢复历史文档,记好4个方法就足够
  • 收银系统源码-千呼新零售2.0【线上营销】
  • OnlyOffice测评
  • UDS - 8 Application layer protocol
  • 二叉树公共最近祖先
  • 智慧运维系统指导规范
  • 最新自助下单彩虹云商城系统源码,含小储云商城模板免授权
  • 头条系统-05-延迟队列精准发布文章-概述添加任务(db和redis实现延迟任务)、取消拉取任务定时刷新(redis管道、分布式锁setNx)
  • .gitignore git添加忽略文件
  • 面向遥感图像的多阶段特征融合目标检测方法
  • 操作系统面试篇一
  • OPenFast软件中的NRELOffshrBsline5MW_Onshore_ServoDyn.dat文件详解
  • 搭建rtmp/rtsp流媒体服务器的步骤
  • vue自定义事件传递数据
  • TensorBoard 安装与启动
  • 云计算运维工程师的突发状况处理
  • 【CSS in Depth 2 精译】1.6 本章小结
  • FFmpeg源码:ff_h2645_extract_rbsp函数分析
  • 关于 AD21导入电子元器件放置“3D体”STEP模型失去3D纹理贴图 的解决方法
  • 【JAVA】利用Redisson和Spring实现高效物联温度控制链路,确保温度调节的准确性和效率,定时链路执行使用案例,一环扣一环
  • yolov8部署资料
  • 迅为RK3588开发板支持LVDS信号,标准 HDMI信号,IMIPI信号
  • 页面开发感想
  • TikTok达人合作ROI分析:品牌如何评估带货效果
  • 硬件实用技巧:电容精度和常用容值表
  • Java面试题:内存管理、类加载机制、对象生命周期及性能优化
  • 什么是 Payment Request API?