微信生活门户网站源码,旅游网站建设案例分析,兴化网页定制,wordpress 正在执行例行维护适配器模式
适配器模式是一种结构型设计模式。作用#xff1a;当接口无法和类匹配到一起工作时#xff0c;通过适配器将接口变换成可以和类匹配到一起的接口。#xff08;注#xff1a;适配器模式主要解决接口兼容性问题#xff09;
适配器的优点与缺点#xff1a;
优…适配器模式
适配器模式是一种结构型设计模式。作用当接口无法和类匹配到一起工作时通过适配器将接口变换成可以和类匹配到一起的接口。注适配器模式主要解决接口兼容性问题
适配器的优点与缺点
优点复用性更强对于那些和目标接口差别不是很大的类通过适配器可以让这些类达到很好的复用减少代码的书写。并且在适配中可以添加一些方法来扩展系统的功能扩展性更好 缺点用多了会让系统看起来杂乱无章。比如明明调用的是接口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 baseRemoteRequestBeannew 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 baseRemoteRequestBeannew 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 baseRemoteRequestBeannew BaseRemoteRequestBean();BeanUtils.copyProperties(remoteRequestBean,baseRemoteRequestBean);return remoteControlService.sendMsg(baseRemoteRequestBean);}Overridepublic Boolean sendSeatMsg(SeatRemoteRequestBean remoteRequestBean) {BaseRemoteRequestBean baseRemoteRequestBeannew BaseRemoteRequestBean();BeanUtils.copyProperties(remoteRequestBean,baseRemoteRequestBean);return remoteControlService.sendMsg(baseRemoteRequestBean);}
}