当前位置:网站首页>适配器模式--能不能好好说话?
适配器模式--能不能好好说话?
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接口的实现,有时候会造成困惑。
适用情况:
- 系统需要使用现有的类,这些类的接口不符合我们的需求,我们又不方便修改其代码。
一般来说,适配器模式可以看作一种“补偿模式”,用来补救设计上的缺陷,或者我们无法修改系统的代码,应用这种模式算是“无奈之举”。
所以我们不能过多的依赖适配器模式。
要适应时代的变化,还得靠自己不断地学习和努力呀。

边栏推荐
- PHP仿网易云原创音乐分享平台网站源码
- js设置ip屏蔽
- Jerry's acquisition of ble distinguishes between reset and wake-up [chapter]
- After 95, programmers in big factories were sentenced for deleting databases! Dissatisfied with the leaders because the project was taken over
- NFT will change data ownership in the metauniverse
- 使用Yolov5训练好模型调用电脑自带摄像头时出现问题:TypeError: argument of type “int‘ is not iterable的解决方法
- How programmers do sidelines
- [CV basis] Color: rgb/hsv/lab
- Leetcode (Sword finger offer) - 10- ii Frog jumping on steps
- Summary of English thesis reading knowledge
猜你喜欢

Cloud image quality assistant IAPP source code

使用Yolov5训练自己制作的数据集,快速上手

DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM

Electron桌面端开发(开发一个闹钟【完结】)

袋鼠云数栈基于CBO在Spark SQL优化上的探索

Probability theory: calculating confidence intervals

云画质助手iApp源码

Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged

地铁路线图云开发小程序源码和配置教程

使用Yolov3训练自己制作数据集,快速上手
随机推荐
Jerry's blepr0 and PR1 are used as ordinary IO ports [chapter]
Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
Using domestic MCU (national technology n32g031f8s7) to realize pwm+dma control ws2812
Jerry's acquisition of ble to check the causes of abnormal conditions such as abnormal code reset [chapter]
杰理之BLE SPP 开启 pin_code 功能【篇】
杰理之BLE SPP 开启 pin_code 功能【篇】
使用RSA与base64对字符串进行加密解密
Probability theory: calculating confidence intervals
Jerry's ble spp open pin_ Code function [chapter]
Digital collection system app source code
C language campus tour guide consultation system
国际多语言出海商城返佣产品自动匹配订单源码
Leetcode 1961. Check whether the string is an array prefix
Jerry's ble spp open pin_ Code function [chapter]
string类的常见构造及容量操作
(key points of software engineering review) Chapter IV overall design exercises
Planck plan 2022 Huawei software elite challenge is coming!
Vscode——vscode 多窗口名字配置成项目名字
[CV basis] Color: rgb/hsv/lab
杰理之获取 BLE 出现电压检测、ADC 检测不准【篇】