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

设计模式十 适配器模式

适配器模式

适配器模式是一种结构型设计模式。作用:当接口无法和类匹配到一起工作时,通过适配器将接口变换成可以和类匹配到一起的接口。(注:适配器模式主要解决接口兼容性问题)

适配器的优点与缺点:

优点:复用性更强,对于那些和目标接口差别不是很大的类,通过适配器可以让这些类达到很好的复用,减少代码的书写。并且在适配中可以添加一些方法来扩展系统的功能,扩展性更好
缺点:用多了会让系统看起来杂乱无章。比如:明明调用的是接口A,结构里面的内容以及被修改成了接口B。一般建议直接对系统进行重构。

适配器模式结构

源(Adaptee):需要被适配的对象或类型。
适配器(Adapter):连接目标和源的中间对象,负责将Adaptee转换为Target。
目标(Target):目标角色,即被转换后的接口。

适配器模式实现方式由三种:类适配器模式、对象适配器模式、接口适配器模式。

三种模式的特点

类适配器模式:类适配器使用的是继承的方式,一般来说无法对其子类进行适配
对象适配器模式:对象适配器使用的是组合的方式,子孙类都可以被适配。另外,对象适配器对于增加一些新行为非常方便,而且新增加的行为同时适用于所有的源。
接口适配器模式(又称缺省适配器模式):接口适配器模式(缺省适配模式)基本思想是,为一个接口提供缺省实现,这样子类可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。

一、类适配器模式

类适配器:即是适配一个类
下发远控命令,使用空调远控命令适配器适配公共远控命令下发
1、源类

public class RemoteControl{@Autowiredprivate KafkaProducer kafkaProducer;@Overridepublic Boolean sendMsg(BaseRemoteRequestBean requestBean) {//业务return kafkaProducer.remoteControl(JSONObject.toJSONString(requestBean));}
}

2、编写适配接口

public interface AirCondAdapter {Boolean sendMsg(AirConditionRemoteRequestBean remoteRequestBean);
}

3、实现适配接口

//适配RemoteControlServiceImpl这个类
@Service
public class RemoteControlAirCondAdapter extends RemoteControlServiceImpl implements AirCondAdapter{@Overridepublic Boolean sendMsg(AirConditionRemoteRequestBean remoteRequestBean) {BaseRemoteRequestBean baseRemoteRequestBean=new BaseRemoteRequestBean();BeanUtils.copyProperties(remoteRequestBean,baseRemoteRequestBean);return super.sendMsg(baseRemoteRequestBean);}
}

二、接口适配器模式

接口适配器:适配接口
1、声明源接口

public interface RemoteControlService {Boolean sendMsg(BaseRemoteRequestBean requestBean);Boolean sendMsg(AirConditionRemoteRequestBean remoteRequestBean);
}

2、实现源接口

public abstract class RemoteControlServiceImpl implements RemoteControlService{@Autowiredprivate KafkaProducer kafkaProducer;@Overridepublic Boolean sendMsg(BaseRemoteRequestBean requestBean) {//业务return kafkaProducer.remoteControl(JSONObject.toJSONString(requestBean));}//适配这个接口public abstract Boolean sendMsg(AirConditionRemoteRequestBean remoteRequestBean);
}

3、适配源接口

@Service
public class RemoteControlAirCondAdapter extends RemoteControlServiceImpl{@Overridepublic Boolean sendMsg(AirConditionRemoteRequestBean remoteRequestBean) {BaseRemoteRequestBean baseRemoteRequestBean=new BaseRemoteRequestBean();BeanUtils.copyProperties(remoteRequestBean,baseRemoteRequestBean);return super.sendMsg(baseRemoteRequestBean);}
}

三、对象适配器模式

对象适配器:适配的是这个对象
1、适配器接口

public interface AirCondAdapter {//下发空调远控指令Boolean sendAirCondMsg(AirConditionRemoteRequestBean remoteRequestBean);//下发座椅远控指令Boolean sendSeatMsg(SeatRemoteRequestBean remoteRequestBean);
}

2、实现适配器

@Service
public class RemoteControlAirCondAdapter implements AirCondAdapter{//适配这个对象@Autowiredprivate RemoteControlService remoteControlService;@Overridepublic Boolean sendAirCondMsg(AirConditionRemoteRequestBean remoteRequestBean) {BaseRemoteRequestBean baseRemoteRequestBean=new BaseRemoteRequestBean();BeanUtils.copyProperties(remoteRequestBean,baseRemoteRequestBean);return remoteControlService.sendMsg(baseRemoteRequestBean);}@Overridepublic Boolean sendSeatMsg(SeatRemoteRequestBean remoteRequestBean) {BaseRemoteRequestBean baseRemoteRequestBean=new BaseRemoteRequestBean();BeanUtils.copyProperties(remoteRequestBean,baseRemoteRequestBean);return remoteControlService.sendMsg(baseRemoteRequestBean);}
}
http://www.lryc.cn/news/91546.html

相关文章:

  • 1.6 初探JdbcTemplate操作
  • 为什么要用线程池?
  • c语言的预处理和编译
  • 网络安全必学 SQL 注入
  • Docker基础知识详解
  • 腾讯、阿里入选首批“双柜台证券”,港股市场迎盛夏升温?
  • CentOS7 使用Docker 安装MySQL
  • 注解和反射复习
  • RocketMQ的demo代码
  • C++ 连接、操作postgreSQL(基于libpq库)
  • Node.js技术简介及其在Web开发中的应用
  • 时间序列分析:原理与MATLAB实现
  • mysql排序之if(isnull(字段名),0,1),字段名 或者 if(isnull(字段名),1,0),字段名
  • 华为OD机试真题 Java 实现【递增字符串】【2023Q1 200分】,附详细解题思路
  • 合并文件解决HiveServer2内存溢出方案
  • 韧性数据安全体系缘起与三个目标 |CEO专栏
  • 华为OD机试真题 Java 实现【火车进站】【牛客练习题】
  • c#快速入门(下)
  • 基于深度学习的目标姿态检测方法_kaic
  • Pycharm设置Python每个文件开头自定义模板(带上声明字符编码、作者名、时间等)
  • Gem相关操作命令
  • 软件测试2023年行情怎么样?仔细讲解!
  • 【1130. 叶值的最小代价生成树】
  • Linux各个目录的全称及含义
  • Cookie和Session原理详解
  • 小程序自动化测试
  • 【linux系统操作】 - 技术一览
  • yield和sleep 区别
  • Redis 注册服务,自动启动
  • 华为OD机试真题 Java 实现【字符统计】【2023 B卷 100分】