当前位置:网站首页>适配器模式--能不能好好说话?
适配器模式--能不能好好说话?
2022-06-11 10:54:00 【zhanyd】
代沟
同学们从小到大,肯定听习惯了父母的唠叨,我们耳朵都磨出了茧,但是他们还是会每天在耳边说个不停。
仿佛一句话,说一万遍,就会变成真理。
也许这就是代沟吧,我们先看看父母都是怎么说的。
public interface Communication {
/** * 关于工作 */
public void aboutWork();
/** * 关于吃饭 */
public void aboutEate();
/** * 关于睡觉 */
public void aboutSleep();
}
public class NormalCommunication implements Communication{
/** * 关于工作 */
@Override
public void aboutWork() {
System.out.println("关于工作:好好工作,不要乱花钱,多存点钱。");
}
/** * 关于吃饭 */
@Override
public void aboutEate() {
System.out.println("关于垃圾食品:不要天天吃外卖,夜宵,要保重身体,注意饮食健康。");
}
/** * 关于睡觉 */
@Override
public void aboutSleep() {
System.out.println("关于睡觉:不要熬夜,要早点睡。");
}
}
public class Test {
public static void main(String[] args) {
System.out.println("家长式的唠叨******");
NormalCommunication normalCommunication = new NormalCommunication();
normalCommunication.aboutEate();
normalCommunication.aboutWork();
normalCommunication.aboutSleep();
}
}
家长式的唠叨******
关于垃圾食品:不要天天吃外卖,夜宵,要保重身体,注意饮食健康。
关于工作:好好工作,不要乱花钱,多存点钱。
关于睡觉:不要熬夜,要早点睡。
新的方式
时光如梭,光阴似箭,转眼间,我们自己也成了父母,免不了也要对自己的孩子”唠叨“,不,沟通。
我们肯定要比我们自己父母当年说得好听点,有效率点,有气势点。
我想了很久,觉得应该这么说:
public class EffectiveCommunication {
/** * 更好地工作 */
public void betterWork() {
System.out.println("关于工作:不赚钱,还想有女朋友/男朋友?想一辈子做单身狗?");
}
/** * 更好地吃饭 */
public void betterEate() {
System.out.println("关于垃圾食品:都胖成猪了,还吃?");
}
/** * 更好地睡觉 */
public void betterSleep() {
System.out.println("关于睡觉:早起的人更帅/更美哦。");
}
}
虽然这一套说辞很完美,充分体现了生活的智慧和沟通的技巧。
但是,EffectiveCommunication类里的方法和Communication接口并不匹配,传统美德不能丢,我们新的沟通方式要和以前的沟通方式兼容才行。
适配器模式
我们生活中最常见的适配器就是电源转换插头,当插座和插头不匹配的时候,就需要加一个转换插头,这个转换插头就是适配器。
这时我们就要请出适配器模式了。
适配器模式(Adapter Pattern) :将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。

我们找一个沟通的适配器,让新的沟通方式能和老的沟通接口相匹配。
public class CommunicationAdpter implements Communication{
private EffectiveCommunication effectiveCommunication;
public CommunicationAdpter(EffectiveCommunication effectiveCommunication) {
this.effectiveCommunication = effectiveCommunication;
}
/** * 关于工作 */
@Override
public void aboutWork() {
effectiveCommunication.betterWork();
}
/** * 关于吃饭 */
@Override
public void aboutEate() {
effectiveCommunication.betterEate();
}
/** * 关于睡觉 */
@Override
public void aboutSleep() {
effectiveCommunication.betterSleep();
}
}
public class Test {
public static void main(String[] args) {
System.out.println("和年轻人的沟通方式******");
CommunicationAdpter communicationAdpter = new CommunicationAdpter(new EffectiveCommunication());
communicationAdpter.aboutEate();
communicationAdpter.aboutWork();
communicationAdpter.aboutSleep();
}
}
和年轻人的沟通方式******
关于垃圾食品:都胖成猪了,还吃?
关于工作:不赚钱,还想有女朋友/男朋友?想一辈子做单身狗?
关于睡觉:早起的人更帅/更美哦。
虽然还是按以前的套路,但是通过适配器,说出了完全不一样的话,还真是符合时代的潮流呢。
总结
Communication接口对应类图中的Traget类,CommunicationAdpter就是类图中的Adpter,EffectiveCommunication就是类图中的Adptee。
客户端本来和Adptee不兼容,不能直接调用,但是有了适配器Adpter,客户端只要调用Adpter,Adpter则是通过调用Adptee对象完成任务,这样就让原来不兼容的两个系统能够合作了。
适配器模式的优点:
- 将目标类和适配者类解耦,通过引入一个适配器类来重用现有的适配者类,而无须修改原有代码。
- 增加了类的透明性和复用性,将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性。
缺点:
- 过多的使用适配器,会让系统变得混乱,比如,明明看到调用的是A接口,其实内部被适配成了B接口的实现,有时候会造成困惑。
适用情况:
- 系统需要使用现有的类,这些类的接口不符合我们的需求,我们又不方便修改其代码。
一般来说,适配器模式可以看作一种“补偿模式”,用来补救设计上的缺陷,或者我们无法修改系统的代码,应用这种模式算是“无奈之举”。
所以我们不能过多的依赖适配器模式。
要适应时代的变化,还得靠自己不断地学习和努力呀。

边栏推荐
- 5. read the specified pathname -dirname
- Jerry's acquisition of ble voltage detection and ADC detection inaccuracy [chapter]
- Development of official account system for digital collection app applet
- 2022年安全月各类活动方案汇报(28页)
- 使用Labelimg制作VOC数据集或yolo数据集的入门方法
- DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM
- Using domestic MCU (national technology n32g031f8s7) to realize pwm+dma control ws2812
- Common construction and capacity operation of string class
- 杰理之获取 BLE 查看代码异常复位等异常情况原因【篇】
- 没有财富就不能自由吗?
猜你喜欢

Campus lost and found applet source code can be used for graduation design

云画质助手iApp源码

beginning一款非常优秀的emlog主题

微信云开发Al短视频一键换脸小程序源码

MySQL optimized learning diary 10 - locking mechanism

Leetcode 1995. Statistics special quads (brute force enumeration)

MySQL下载安装使用-完整详细步骤
![Jerry's acquisition of ble to check the causes of abnormal conditions such as abnormal code reset [chapter]](/img/c5/a9468ad75bd6a8776c0695140d1a5d.png)
Jerry's acquisition of ble to check the causes of abnormal conditions such as abnormal code reset [chapter]

概率论:计算置信区间

International multilingual sea mall rebate product automatic matching order source code
随机推荐
Shi Yigong: I was not interested in research until I graduated from my doctor's degree! I'm confused about the future, and I don't know what to do in the future
Writing the program into the microcontroller can control the forward and reverse rotation of the motor more conveniently and quickly
Jszip get the file of the specified file in the uploaded zip package
Golang compilation and linking parameters, runtime
Characteristics and classification of creation mode (single case, factory)
Summary of information of main account of Chia Tai futures on Wednesday in advance
使用Yolov5训练自己制作的数据集,快速上手
PHP仿网易云原创音乐分享平台网站源码
Digital collection app applet official account source code
Why is Web3 a game changer for the "creator economy"
SurroundDepth:自监督多摄像头环视深度估计
距离度量 —— 欧式距离(Euclidean Distance)
封装组件系列-(一)-插槽及动态组件
正大期货主账户预4 周三信息汇总
杰理之获取 BLE 出现电压检测、ADC 检测不准【篇】
云画质助手iApp源码
Where is it safer to open an account for soda ash futures? How much capital is needed to buy soda ash futures?
2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
Interpreting USB3.0 test items
Remote monitoring project offline log specification