当前位置:网站首页>07 adapter mode
07 adapter mode
2022-06-22 08:28:00 【Grow some hair.】
Reprint of the original :https://blog.csdn.net/sinat_21107433/article/details/102656617
You must have heard of “ network adapter ” Well ? Also called network card . What is its function ?—— surf the internet !
Such an answer is obviously not professional , The right answer is “ An important function of network card is serial connection / Parallel transformation . Because the communication between network card and LAN is carried out by serial transmission through cable or twisted pair , The communication between the network card and the computer is through the... On the computer motherboard I/O The bus is transmitted in parallel .”
You must ask :“ What does this have to do with me ?”
Of course there is , Because you're learning design patterns , This is related to the adapter pattern to be introduced in this article !
1. Brief Introduction
In addition to the network card adapter , You must have heard of power adapters ? The voltage of domestic electricity in China is 220V, But our computers 、 mobile phone 、 Flat 、 The razor ( Rechargeable ) You won't use such a high voltage . This requires a power adapter ( The charger 、 transformer ), Make various electronic devices and 220 Supply voltage of compatible . The power adapter acts as an adapter .
In software system design , When the classes that need to be combined are incompatible , Adapters like transformers are also needed to coordinate these incompatibilities , This is the adapter pattern !
So what is the adapter pattern ?
Adapter pattern :
Converts the interface of a class to another interface that the customer wants . The adapter pattern allows classes with incompatible interfaces to work together .
Similar to the power adapter , In the adapter pattern, there will be a design called “ Adapter ” The wrapper class , The object wrapped by the adapter is called Adapter . According to the definition , The adapter is based on the needs of customers , Convert the adapter's existing interface into another interface , Enables incompatible classes to work together .
2. Adapter pattern structure
The adapter pattern is divided into class adapter and object adapter .
- The adapter class (Adapter): There is an inheritance or implementation relationship between the adapter and the adapter ;
- Adapter class (Adaptee): There is an association between adapter and adapter .
- Target abstract class (Target): Define the interface required by the customer .
Class adapter and object adapter UML The graph is as follows . Class adapter , The adapter class inherits the adapter class , And re implement the specific interface of the adapter to achieve the purpose of adapting the interface required by the customer . Object adapter , The adapter class instantiates an object of the adapter class in the class , And encapsulate it in the interface of the function required by the customer , Achieve the final adaptation purpose .

3. Adapter pattern code instance
Jungle The adapter pattern has been used many times in a project . Here is an example of using the object adapter pattern .
Path planning consists of two stages : First, read and parse the drawing file , Get the point 、 Linear coordinates ; Secondly, calculate the processing path according to the demand . Software controller (Controller) On , System click “ Path planning ” The button automatically completes the above process .
Jungle A class has been encapsulated DxfParser, The suffix of the class name can be read dxf Drawing files for , And analyze the points 、 Line , Save to the path list . Another class PathPlanner Used to calculate the machining path .
In this case ,Controller Is the target abstract class ,DxfParser and PathPlanner It's an adapter class , The methods provided by these two classes can be used to meet the requirements of path planning . We just need to define another adapter class Adapter that will do .

3.1. Target abstract class
// Target abstract classclass Controller{public:Controller(){}virtual void pathPlanning() = 0;private:};
3.2. Adapter class
// Adapter class DxfParserclass DxfParser{public:DxfParser(){}void parseFile(){printf(" Parse the file and extract the data \n");}};// Adapter class PathPlannerclass PathPlanner{public:PathPlanner(){}void calculate(){printf(" Calculate machining path \n");}};
3.3. The adapter class
// The adapter class Adapterclass Adapter:public Controller{public:Adapter(){dxfParser = new DxfParser();pathPlanner = new PathPlanner();}void pathPlanning(){printf(" Path planning :\n");dxfParser->parseFile();pathPlanner->calculate();}private:DxfParser *dxfParser;PathPlanner *pathPlanner;};
3.4. Client code example
#include <iostream>#include "AdapterPattern.h"int main(){Controller *controller = new Adapter();controller->pathPlanning();system("pause");return 0;}
3.5. effect

4. Adapter pattern summary
advantage :
- Decouple the target class from the adapter class , Introduce an adapter class to realize code reuse , There is no need to modify the original structure ;
- Increase the transparency and reuse of classes , For clients , The adapter class is transparent ;
- Object adapters can adapt different adapters to the same target ( Object adapter );
shortcoming :
- Restrictions on programming languages :Java Multiple inheritance is not supported , At most one adapter class can be adapted at a time , Cannot fit multiple adapter classes at the same time ;
Apply to the environment :
- The system needs to use some existing classes , But the interfaces of these classes do not meet the needs of the system , Or there is no source code for these classes ;
- Want to create a reusable class , Used to work with classes that are not very related to each other .
边栏推荐
猜你喜欢

Flask博客实战 - 实现文章管理

Carry out effective maker education courses and activities

Remove the restriction of video memory occupied by tensorflow GPU

Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security

16 解释器模式

Interpreting the technology group in maker Education

20 状态模式

Web knowledge 3 (cookie+session)

Coding complexity C (n)

10.File/IO流-bite
随机推荐
Record once · ulimit: open files: cannot modify limit: operation not allowed
Flask博客实战 - 创建后台管理应用
16 解释器模式
Postgresql源码(56)可扩展类型分析ExpandedObject/ExpandedRecord
0 basic self-study STM32 (wildfire) -- what is a register?
Example of multipoint alarm clock
Basic concepts of homomorphic encryption
The necessity of steam education culture inheritance
深入理解MySQL索引凭什么能让查询效率提高这么多?
08 桥接模式
EURUSD,H1: invalid lots amount for OrderSend function
07 适配器模式
邮件巨头曝严重漏洞,用户数据被窃取
Mysql5.7 master-slave mode reference
20 状态模式
Basic knowledge and practical application of redis
Mysql+orcle (SQL implements recursive query of all data of child nodes)
Crawling microblog comments | emotional analysis of comment information | word cloud of comment information
QT error prompt 1:invalid use of incomplete type '***‘
Some problems encountered in using swagger in golang