当前位置:网站首页>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

 
  1. // Target abstract class

  2. class Controller

  3. {

  4. public:

  5. Controller(){}

  6. virtual void pathPlanning() = 0;

  7. private:

  8. };

3.2. Adapter class

 
  1. // Adapter class DxfParser

  2. class DxfParser

  3. {

  4. public:

  5. DxfParser(){}

  6. void parseFile(){

  7. printf(" Parse the file and extract the data \n");

  8. }

  9. };

  10.  
  11. // Adapter class PathPlanner

  12. class PathPlanner

  13. {

  14. public:

  15. PathPlanner(){}

  16. void calculate(){

  17. printf(" Calculate machining path \n");

  18. }

  19. };

3.3. The adapter class

 
  1. // The adapter class Adapter

  2. class Adapter:public Controller

  3. {

  4. public:

  5. Adapter(){

  6. dxfParser = new DxfParser();

  7. pathPlanner = new PathPlanner();

  8. }

  9. void pathPlanning(){

  10. printf(" Path planning :\n");

  11. dxfParser->parseFile();

  12. pathPlanner->calculate();

  13. }

  14. private:

  15. DxfParser *dxfParser;

  16. PathPlanner *pathPlanner;

  17. };

3.4. Client code example

 
  1. #include <iostream>

  2. #include "AdapterPattern.h"

  3.  
  4. int main()

  5. {

  6. Controller *controller = new Adapter();

  7. controller->pathPlanning();

  8.  
  9. system("pause");

  10. return 0;

  11. }

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 .
原网站

版权声明
本文为[Grow some hair.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220821493834.html