当前位置:网站首页>Adapter mode -- can you talk well?
Adapter mode -- can you talk well?
2022-06-11 11:13:00 【zhanyd】
Generation gap
Students from small to large , I must be used to listening to my parents' nagging , Our ears are all cocooned , But they still keep talking in their ears every day .
Like a word , Say it ten thousand times , Will become the truth .
Maybe this is the generation gap , Let's see what our parents say first .
public interface Communication {
/** * About work */
public void aboutWork();
/** * About eating */
public void aboutEate();
/** * About sleeping */
public void aboutSleep();
}
public class NormalCommunication implements Communication{
/** * About work */
@Override
public void aboutWork() {
System.out.println(" About work : Work hard , Don't spend money , Save more money .");
}
/** * About eating */
@Override
public void aboutEate() {
System.out.println(" About junk food : Don't eat takeout every day , Food taken late at night , Take care of your body , Pay attention to healthy diet .");
}
/** * About sleeping */
@Override
public void aboutSleep() {
System.out.println(" About sleeping : Don't stay up late , Go to bed early .");
}
}
public class Test {
public static void main(String[] args) {
System.out.println(" Paternalistic nagging ******");
NormalCommunication normalCommunication = new NormalCommunication();
normalCommunication.aboutEate();
normalCommunication.aboutWork();
normalCommunication.aboutSleep();
}
}
Paternalistic nagging ******
About junk food : Don't eat takeout every day , Food taken late at night , Take care of your body , Pay attention to healthy diet .
About work : Work hard , Don't spend money , Save more money .
About sleeping : Don't stay up late , Go to bed early .
A new way
Time passed quickly , Time flies like an arrow , Twinkling of an eye , We have become parents ourselves , You have to treat your own children ” To nag “, No , communicate .
We must speak better than our own parents did , Be efficient , Have momentum .
I thought about it for a long time , I think I should say so :
public class EffectiveCommunication {
/** * Work better */
public void betterWork() {
System.out.println(" About work : No money , Also want to have a girlfriend / Boy friend ? I want to be a single dog all my life ?");
}
/** * Eat better */
public void betterEate() {
System.out.println(" About junk food : All fat into a pig , And eat ?");
}
/** * Better sleep */
public void betterSleep() {
System.out.println(" About sleeping : Early risers are more handsome / More beautiful .");
}
}
Although this set of words is perfect , It fully reflects the wisdom of life and communication skills .
however ,EffectiveCommunication Methods in classes and Communication Interface does not match , Traditional virtues cannot be lost , Our new way of communication should be compatible with the previous way of communication .
Adapter pattern
The most common adapter in our life is the power conversion plug , When the socket and plug do not match , You need to add a conversion plug , This adapter plug is the adapter .
At this point, we will ask for the adapter mode .
Adapter pattern (Adapter Pattern) : Convert one interface to another that the customer wants , The adapter pattern enables classes that are not compatible with the interface to work together , It is alias wrapper (Wrapper).

Let's find a communication adapter , Make the new communication mode match the old communication interface .
public class CommunicationAdpter implements Communication{
private EffectiveCommunication effectiveCommunication;
public CommunicationAdpter(EffectiveCommunication effectiveCommunication) {
this.effectiveCommunication = effectiveCommunication;
}
/** * About work */
@Override
public void aboutWork() {
effectiveCommunication.betterWork();
}
/** * About eating */
@Override
public void aboutEate() {
effectiveCommunication.betterEate();
}
/** * About sleeping */
@Override
public void aboutSleep() {
effectiveCommunication.betterSleep();
}
}
public class Test {
public static void main(String[] args) {
System.out.println(" Communication with young people ******");
CommunicationAdpter communicationAdpter = new CommunicationAdpter(new EffectiveCommunication());
communicationAdpter.aboutEate();
communicationAdpter.aboutWork();
communicationAdpter.aboutSleep();
}
}
Communication with young people ******
About junk food : All fat into a pig , And eat ?
About work : No money , Also want to have a girlfriend / Boy friend ? I want to be a single dog all my life ?
About sleeping : Early risers are more handsome / More beautiful .
Although still according to the previous routine , But through the adapter , Said something completely different , It really conforms to the trend of the times .
summary
Communication The interface corresponds to... In the class diagram Traget class ,CommunicationAdpter In the class diagram Adpter,EffectiveCommunication In the class diagram Adptee.
The client and Adptee Are not compatible , Can't call directly , But with adapters Adpter, The client just calls Adpter,Adpter By calling Adptee Object to complete the task , In this way, the two incompatible systems can cooperate .
Advantages of adapter mode :
- Decouple the target class from the adapter class , Reuse existing adapter classes by introducing an adapter class , Without modifying the original code .
- Increased class transparency and reusability , Encapsulate the concrete implementation in the adapter class , It's transparent to client classes , And it improves the reusability of the adapter .
shortcoming :
- Excessive use of adapters , It will make the system chaotic , such as , Obviously, what we call is A Interface , In fact, the interior is adapted into B Interface implementation , Sometimes it can cause confusion .
Application :
- The system needs to use existing classes , The interfaces of these classes do not meet our requirements , It is not convenient for us to modify the code .
Generally speaking , The adapter pattern can be seen as a kind of “ Compensation mode ”, To remedy design defects , Or we can't change the system code , Applying this model is “ Helpless move ”.
So we can't rely too much on the adapter pattern .
To adapt to the changes of the times , Still have to rely on their own continuous learning and hard work .

边栏推荐
- 想做钢铁侠?听说很多大佬都是用它入门的
- Leetcode (Sword finger offer) - 10- ii Frog jumping on steps
- 李飞飞:我更像物理学界的科学家,而不是工程师|深度学习崛起十年
- Introduction to database system -- Chapter 2 -- relational database (2.4 relational algebra)
- Iterator mode -- battlefield autumn point
- Rxjs Observable.pipe 传入多个 operators 的执行逻辑分析
- 拆分数据---水平拆分和纵向拆分
- 2022 Health Expo, Beijing Great Health Industry Exhibition, moxibustion health exhibition, Beijing Health Service Exhibition
- VOC格式数据集转yolo格式数据集的方法
- 设置默认收货地址【项目 商城】
猜你喜欢

Interpreting USB3.0 test items
![Jerry's acquisition of ble distinguishes between reset and wake-up [chapter]](/img/cb/e127628f20c36abe3287a175ae5948.png)
Jerry's acquisition of ble distinguishes between reset and wake-up [chapter]

MySQL download, installation and use - complete and detailed steps
![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]

985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci

Iterator mode -- battlefield autumn point

杰理之获取 BLE 查看代码异常复位等异常情况原因【篇】

装饰模式--小美的生日蛋糕

MyCat-分库分表

SpingBoot+Quartrz生产环境的应用支持分布式、自定义corn、反射执行多任务
随机推荐
2022北京国际营养健康产业博览会,第九届中国大健康产业展会
文件excel导出
使用Yolov5训练自己制作的数据集,快速上手
Appearance mode -- it has been used in various packages for a long time!
Using domestic MCU (national technology n32g031f8s7) to realize pwm+dma control ws2812
Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
1712. 将数组分成三个子数组的方案数 ●●
Electron desktop development (development of an alarm clock [End])
AcWing 1944. 记录保存(哈希,STL)
Writing the program into the microcontroller can control the forward and reverse rotation of the motor more conveniently and quickly
施一公:我直到博士毕业,对研究也没兴趣!对未来很迷茫,也不知道将来要干什么......
Typeerror: argument of type "Int 'is not Iterable
Store management skills: how to manage chain stores efficiently?
Yibentong 1122: calculating saddle point
Development of official account system for digital collection app applet
Common construction and capacity operation of string class
(key points of software engineering review) Chapter IV overall design exercises
Digital collection app applet official account source code
数据库系统概论 ---- 第二章 -- 关系数据库(2.4 关系代数)
Update failed to update bytea type PostgreSQL