Adapter mode and C ++ implementation

Source: Internet
Author: User
Adapter Mode

The adapter mode is a very understandable mode, which is also very common in life. What Plug 2 ports to 3 Ports, what USB to PS2, This is the adapter mode. To put it bluntly, if you have some interfaces that provide a lot of functionality, but you don't have a good interface to use, you need an adapter, convert the interface you need to your own. This is also obvious, that is, you do not need to change your current interface, so that you do not need to modify your usage anywhere, then, the underlying implementation is implemented by the adapter calling the required interface.

 

Common scenarios

When using a third-party library, the third-party library certainly cannot apply to all systems, so an adapter is needed for conversion.

 

Advantages

1. Blocking specific implementation methods and implementing Dependency inversion.

2. You can encapsulate inconsistent interfaces to make them a unified interface.

3. Convert unsuitable interfaces into unified interfaces.

 

Disadvantages

 

C ++ implementation

 

 1   # Ifndef _ adapter_h _  2   # Define _ Adapter_h _ 3  4 # Include "  Adaptee. h  "  5   6   7   Class  Target  8   {  9   Public  :  10   Target ();  11      Virtual ~ Target ();  12   13       Virtual   Void Request () = 0  ;  14   };  15   16   17   Class Adapter: Public  Target 18   {  19   Public  :  20   Adapter ();  21 ~ Adapter ();  22   23       Void  Request ();  24   25   26   Private :  27 Adaptee * Adaptee;  28   };  29   30   31   # Endif 
 1 # Include "  Adapter. h  "  2   3   4   Target: Target () 5   {  6   7   }  8   9   10 Target ::~ Target ()  11   {  12   13   }  14   15   16  Adapter: adapter ():  17 Adaptee ( New  Adaptee ())  18   {  19   20   }  21   22   23 Adapter ::~ Adapter ()  24   {  25   26  }  27   28   29   Void  Adapter: Request ()  30   {  31 Adaptee-> Specificrequest ();  32 }
 1   # Ifndef _ adaptee_h _  2   # Define _ Adaptee_h _ 3  4   Class  Adaptee  5   {  6   Public  :  7   Adaptee ();  8 ~ Adaptee ();  9   10       Void  Specificrequest ();  11  12   };  13   14   15   # Endif 
 1 # Include "  Adaptee. h  "  2 # Include <stdio. h> 3   4   5   Adaptee: adaptee ()  6   { 7   8   }  9   10   11 Adaptee ::~ Adaptee ()  12   {  13   14   }  15   16   17   Void  Adaptee: specificrequest () 18   {  19 Fprintf (stderr, "  This is specificrequest \ n  "  );  20 }
  1  # include "   adapter. h   "  2   3   4   int   main ()   5  {  6  Target * tar =  New   adapter ();   7  tar->  request ();   8   return   0  ;  9 }
1G ++-O client.CPPAdapter.CPPAdaptee.CPP

 

Running result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.