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

边栏推荐
- JS set IP mask
- MySQL download, installation and use - complete and detailed steps
- 使用RSA与base64对字符串进行加密解密
- Mxnet construction and implementation of alexnet model (comparison with lenet)
- 2022年最好的年金险产品是什么?
- Store management skills: how to manage chain stores efficiently?
- [K-means] K-means learning examples
- 2022 Beijing International Nutrition and Health Industry Expo, the 9th China Great Health Industry Exhibition
- Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
- 95后大厂程序员删库被判刑!只因项目被接手对领导心生不满
猜你喜欢
![[games101] operation 2 -- triangle rasterization](/img/43/1a58206b8b7e6d19d2944cdb7bb0dc.png)
[games101] operation 2 -- triangle rasterization

数据库系统概论 ---- 第二章 -- 关系数据库(2.4 关系代数)

MySQL (IX)

MySQL optimized learning diary 10 - locking mechanism

MN梦奈宝塔主机系统V1.5版本发布

SurroundDepth:自监督多摄像头环视深度估计
![Jerry's acquisition of ble OTA dual backup upgrade (can only be used for chips above 4mbits) [article]](/img/25/e90d23f5c3d2aa6790538b289137bf.png)
Jerry's acquisition of ble OTA dual backup upgrade (can only be used for chips above 4mbits) [article]

NFT will change data ownership in the metauniverse

Characteristics and classification of creation mode (single case, factory)

Mxnet construction and implementation of alexnet model (comparison with lenet)
随机推荐
985高校博士因文言文致谢走红!导师评价其不仅SCI写得好...
Jerry's acquisition of ble voltage detection and ADC detection inaccuracy [chapter]
小 P 周刊 Vol.08
C language course design
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
启牛帮开通的证券账户是安全可信的吗?
Leetcode 1961. Check whether the string is an array prefix
Droid-slam: depth vision slam for monocular and binocular rgbd cameras
PHP仿网易云原创音乐分享平台网站源码
云画质助手iApp源码
Jerry's ble spp open pin_ Code function [chapter]
2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
C language campus tour guide consultation system
SWUST oj668: the thief ran away
使用 Hystrix 实现微服务的容错处理
Content-Type: multipart/form-data; boundary=${bound}
Characteristics and classification of creation mode (single case, factory)
使用RSA与base64对字符串进行加密解密
2022 Health Expo, Beijing Great Health Industry Exhibition, moxibustion health exhibition, Beijing Health Service Exhibition
95后大厂程序员删库被判刑!只因项目被接手对领导心生不满