当前位置:网站首页>结构型模式-适配器模式
结构型模式-适配器模式
2022-07-27 03:46:00 【vbirdbest】
一:定义
将一个类的接口转换成客户希望的另外一个接口,使得原来由于接口不兼容而不能一起工作的那些类能一起工作。
适配器Adapter包含的主要角色
- 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。标准的交流电源插头。
- 适配者(Adaptoo)类:它是被访问和适配器的现存组件库中的组件接口。欧洲墙视插座。
- 适配器(Adapter)类:它是一个转换器,通过继承或者引用适配者对象,把适配者接口转成目标接口,让客户按目标接口的格式访问适配器。交流电适配器。
二:类适配器模式
我们的电脑只能读取SD卡,现在我们有一张TF卡也需要使用电脑读取出来。此时我们就需要增加一个适配器来适配TF卡。
适配者:SD卡。
/** * 适配者 */
public interface SDCard {
String readSD();
void writeSD(String msg);
}
public class SDCardImpl implements SDCard {
@Override
public String readSD() {
return "read sd card";
}
@Override
public void writeSD(String msg) {
System.out.println("write msg to sd card");
}
}
目标:TF卡。
public interface TFCard {
String readTF();
void writeTF(String msg);
}
public class TFCardImpl implements TFCard {
@Override
public String readTF() {
return "read tf card";
}
@Override
public void writeTF(String msg) {
System.out.println("write msg to tf card");
}
}
适配器类:需要继承目标实现类TFCardImpl,并且实现适配者接口SDCard。内部实现最终还是调用的是目标实现类的方法。
public class TF2SDCardAdapter extends TFCardImpl implements SDCard {
@Override
public String readSD() {
return this.readTF();
}
@Override
public void writeSD(String msg) {
this.writeTF(msg);
}
}
电脑Computer
public class Computer {
public String readSD(SDCard sdCard) {
return sdCard.readSD();
}
}
Client
public class Client {
public static void main(String[] args) {
Computer computer = new Computer();
String sdCardMsg = computer.readSD(new SDCardImpl());
System.out.println(sdCardMsg);
// 读取TF卡
String tfCardMsg = computer.readSD(new TF2SDCardAdapter());
System.out.println(tfCardMsg);
}
}
三:对象适配器模式
类适配器模式 TF2SDCardAdapter extends TFCardImpl 需要继承目标实现类,java是单继承的,如果继承了目标实现类那么适配器类就不能再继承其它类了,这是类适配器不好的地方。
对象适配器模式可以采用将现有组件库中的已经实现的组件引入适配类中,该类同时实现当前系统的业务接口。
对象适配器模式不需要继承,只需要将目标对象引入作为属性即可。
public class TFToSDCardAdapter implements SDCard {
// 目标接口
private TFCard tfCard;
// 只提供一个有参构造函数
public TFToSDCardAdapter(TFCard tfCard) {
this.tfCard = tfCard;
}
@Override
public String readSD() {
return tfCard.readTF();
}
@Override
public void writeSD(String msg) {
tfCard.writeTF(msg);
}
}
Client
public class Client {
public static void main(String[] args) {
Computer computer = new Computer();
String sdCardMsg = computer.readSD(new SDCardImpl());
System.out.println(sdCardMsg);
// 读取TF卡
String tfCardMsg = computer.readSD(new TFToSDCardAdapter(new TFCardImpl()));
System.out.println(tfCardMsg);
}
}
四:应用场景
- 以前开发的系统存在满足新系统功能需求的接口,但其接口同新系统的接口不一致,需要转换一下。
- 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。
五:适配器模式简介
适配器模式其实就是对目标对象的一种包装,然后暴露出来统一的方法签名而已。
边栏推荐
- BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
- F - Pre-order and In-order(Atcoder 255)
- 一张图看懂KingbaseES V9
- Plato Farm全新玩法,套利ePLATO稳获超高收益
- STM32 serial port based on Hal library accepts interrupts and idle interrupts
- webpack打包vue项目添加混淆方式,解决缓存问题
- 【比赛参考】PyTorch常用代码段以及操作合集
- Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools
- Wechat input component adds a clear icon, and clicking clear does not take effect
- 法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
猜你喜欢

Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content

网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看

Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)

Delete the whole line of Excel, and delete the pictures together

tcp协议知识详解

Practice of microservice in solving Library Download business problems
![[C language] recursively explain the tower of Hanoi problem](/img/a6/bbf1f19fc2a663df155cca53f2538a.png)
[C language] recursively explain the tower of Hanoi problem

JMeter download and installation

Convolution neural network -- a detailed introduction to convolution of 24 bit color images

佳明手表怎么设置用户定制显示
随机推荐
Anonymous named pipes, understanding and use of interprocess communication in shared memory
第六章:云数据库
【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)
C get UUID
Okaleido ecological core equity Oka, all in fusion mining mode
Some common instructions in JVM tuning
利用JSON类型在mysql中实现数组功能
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
The data in echart histogram is displayed at the top of the chart
How to set user-defined display for Jiaming Watch
Brightcove任命Dan Freund为首席营收官
卷积神经网络——24位彩色图像的卷积的详细介绍
Navicat exports Mysql to table structure and field description
BigDecimal踩坑总结&最佳实践
Wechat applet rotation map
F - Pre-order and In-order(Atcoder 255)
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
PX4模块设计之十二:High Resolution Timer设计
Elastic certification test: 30 day FastPass Study Guide
Sed output specified line