1、概述
适配器模式是一种结构型设计模式, 它能使接口不兼容的对象能够相互合作。
2、适用场景
1)当你希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用适配器类。
2)如果您需要使用这样一些类, 他们处于同一个继承体系, 并且他们又有了额外的一些共同的方法, 但是这些共同的方法不是所有在这一继承体系中的子类所具有的共性。可以将这些方法封装在一个装饰器中。
3、实例
有以下场景:
方钉适配圆孔的适配器,方钉想放到圆孔中,则圆孔的直径等于方钉的对角长度。
定义方钉、圆孔
圆孔:
直径
圆钉:
直径
方钉:
边长
定义方钉:
/**
* 圆钉
* @date: 2021/1/7
* @author weirx
* @version 3.0
*/
public class SquareNails {
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public SquareNails(double width) {
this.width = width;
}
/**
* 边长
*/
private double width;
}
定义圆钉:
/**
* 圆钉
* @date: 2021/1/7
* @author weirx
* @version 3.0
*/
public class RoundNails {
/**
* 直径
*/
private double diameter;
public double getDiameter() {
return diameter;
}
public void setDiameter(double diameter) {
this.diameter = diameter;
}
public RoundNails(double diameter) {
this.diameter = diameter;
}
}
定义圆孔:
/**
* 圆孔
* @date: 2021/1/7
* @author weirx
* @version 3.0
*/
public class RoundHold {
/**
* 直径
*/
private double diameter;
public RoundHold(double diameter) {
this.diameter = diameter;
}
public double getDiameter() {
return diameter;
}
public void setDiameter(double diameter) {
this.diameter = diameter;
}
/**
* 校验是否合适
* @param roundNails
* @return
*/
public boolean fits(RoundNails roundNails){
if (diameter >= roundNails.getDiameter()){
return true;
}else {
return false;
}
}
}
定义适配器:
/**
* 方钉适配圆洞适配器
* @date: 2021/1/7
* @author weirx
* @version 3.0
*/
public class SquareNailsRoundHoldAdapter {
public RoundNails getResult(SquareNails squareNails){
double width = squareNails.getWidth();
double diagonal = width * Math.sqrt(2);
RoundNails roundNails = new RoundNails(diagonal);
return roundNails;
}
}
测试类:
/**
* 测试
* @date: 2021/1/7
* @author weirx
* @version 3.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BsspUserApplication.class)
public class TestDemo {
@Test
public void test() {
//定义个圆孔
RoundHold roundHold = new RoundHold(10);
//定义圆钉
RoundNails roundNails = new RoundNails(10);
//定义方钉,边距10
SquareNails squareNails10 = new SquareNails(10);
//定义方钉,边距6
SquareNails squareNails6 = new SquareNails(6);
//适配器
SquareNailsRoundHoldAdapter squareNailsRoundHoldAdapter = new SquareNailsRoundHoldAdapter();
RoundNails result10 = squareNailsRoundHoldAdapter.getResult(squareNails10);
RoundNails result6 = squareNailsRoundHoldAdapter.getResult(squareNails6);
//圆钉是否合适
if (roundHold.fits(roundNails)) {
System.out.println("this round nails is fits");
} else {
System.out.println("this round nails is does not fits");
}
//10方钉是否合适
if (roundHold.fits(result10)) {
System.out.println("squareNails10 is fits");
} else {
System.out.println("squareNails10 is does not fits");
}
//6方钉是否合适
if (roundHold.fits(result6)) {
System.out.println("squareNails6 is fits");
} else {
System.out.println("squareNails6 is does not fits");
}
}
}
结果:
this round nails is fits
squareNails10 is does not fits
squareNails6 is fits
4、总结
优点:
1)单一原则:将代码或者数据转换的过程从主要业务逻辑区分出来。
2)开闭原则:只要客户端代码通过客户端接口与适配器进行交互, 你就能在不修改现有客户端代码的情况下在程序中添加新类型的适配器。
缺点:
增加代码复杂度。使用时需要考虑是否在原功能上修改更加简单。
网友评论