适配器模式(Adapter ),对接口进行转换,以达到适用的目的.常常是接口之前的一种转换机制,让不同的接口能够满足使用情况,达到适配的目的.常用例子就是电源插座了.自行复习吧.
作者是个二吊子,如果描述有误请指出.
使用场景
我们在日常挖坑中,会遇到要对一些接口进行转化,因为不满足使用情况,我们需要对其中的过程进行处理.或者修改行为,就能用到适配器模式.适配器模式是对接口隔离的验证.
类适配器
uml图
提供统一的调用入口,通过继承原接口类,实现进行整合.这个里面用到了继承,如前文所讲,设计模式并不是很提倡继承的使用,所以就有了后边的对象适配器来替代
/**
* @Description: 220v电压,被适配类
* @Author: gaofan
* @Date: 2020/3/21 17:41
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class Volatge220V {
public int output(){
int src = 220;
System.out.println("电压 = " + src + "伏");
return src;
}
}
/**
* @Description: 适配接口
* @Author: gaofan
* @Date: 2020/3/21 17:42
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public interface IVolatge5V {
public int output5V();
}
/**
* @Description: 适配器类
* @Author: gaofan
* @Date: 2020/3/21 17:43
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class VolatgeAdapter extends Volatge220V implements IVolatge5V {
@Override
public int output5V() {
// 获取220v电压
int src = output();
int dest = src - 215;
System.out.println("适配了5V电压");
return dest;
}
}
/**
* @Description: TODO
* @Author: gaofan
* @Date: 2020/3/21 17:45
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class Phone {
// 充电
public void charging(IVolatge5V iVolatge5V){
if(iVolatge5V.output5V() == 5){
System.out.println("电压5伏,充电中.....");
}else {
System.out.println("电压异常,爆炸....");
}
}
public static void main(String [] args){
Phone phone = new Phone();
phone.charging(new VolatgeAdapter());
for(int i = 0;i<100;i++){
System.out.print(".");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
类型: 类适配器
原理: 通过定义抽象接口,适配器类继承目标类,实现抽象接口,适配器类可能显式使用了目标类的方法来进行处理.我们在进行调用的时候使用抽象接口多态.
缺点: 显式使用了目标类的方法,使用了继承,java是单根继承.产生强耦合.希望同构聚合和组合方式改善
对象适配器
通过实现接口和持有目标对象,通过对象调用,无继承,合成复用原则
uml图
这里主要是把继承关系改为了聚合后者组合的方式来进行处理的,小改进
/**
* @Description: 220v电压,被适配类
* @Author: gaofan
* @Date: 2020/3/21 17:41
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class Volatge220V {
public int output(){
int src = 220;
System.out.println("电压 = " + src + "伏");
return src;
}
}
/**
* @Description: 适配接口
* @Author: gaofan
* @Date: 2020/3/21 17:42
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public interface IVolatge5V {
public int output5V();
}
/**
* @Description: 适配器类
* @Author: gaofan
* @Date: 2020/3/21 17:43
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class VolatgeAdapter implements IVolatge5V {
private Volatge220V volatge220V;
public VolatgeAdapter(Volatge220V volatge220V) {
this.volatge220V = volatge220V;
}
@Override
public int output5V() {
// 获取220v电压
int src = volatge220V.output();
int dest = src - 215;
System.out.println("适配了5V电压");
return dest;
}
}
/**
* @Description: TODO
* @Author: gaofan
* @Date: 2020/3/21 17:45
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class Phone {
// 充电
public void charging(IVolatge5V iVolatge5V){
if(iVolatge5V.output5V() == 5){
System.out.println("电压5伏,充电中.....");
}else {
System.out.println("电压异常,爆炸....");
}
}
public static void main(String [] args){
Phone phone = new Phone();
phone.charging(new VolatgeAdapter(new Volatge220V()));
for(int i = 0;i<100;i++){
System.out.print(".");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
类型: 对象适配器
原理: 在类适配器的基础上进行了改进,抛弃了继承,使用聚合组合关系,减少了耦合性
接口适配器
缺省适配器模式,当不需要实现全部方法时,新建抽象类实现接口,然后适配器继承抽象类实现部分接口的方式
uml图
接口适配器多用于,如果使用到一个接口,而接口中有很多接口未实现,我们如果实现该接口就需要重写全部的方法,然而我们不一定会用到其他的方法.这时候可以使用接口适配器,建立一个抽象类实现接口以及其中的方法,可以空实现.然后我们的目标接口直接继承这个抽象类就可以了,只需要实现需要实现的方法.也可以通过内部类选择实现接口.这是接口隔离的原则.
/**
* @Description: 源接口类
* @Author: gaofan
* @Date: 2020/3/21 18:24
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public interface IInterface {
public void start();
public void run();
public void waitTime();
public void block();
public void ternated();
}
/**
* @Description: 适配器
* @Author: gaofan
* @Date: 2020/3/21 18:25
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class AbsAdapter implements IInterface {
@Override
public void start() {
}
@Override
public void run() {
}
@Override
public void waitTime() {
}
@Override
public void block() {
}
@Override
public void ternated() {
}
}
/**
* @Description: 目标类
* @Author: gaofan
* @Date: 2020/3/21 18:26
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class DestClass extends AbsAdapter {
@Override
public void start() {
System.out.print("start");
}
@Override
public void run() {
System.out.print("start");
}
}
类型: 接口适配器
原理: 接口隔离的原则实现
本文借鉴尚硅谷Java设计模式,韩顺平图解java,传送门