Adapter Adapter Mode
function : Transforms the interface of a class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
Divided into class adapter mode and object adapter mode.
The data and behavior of the system are correct, but when the interface does not match, we should consider using an adapter to match an existing object outside the control range with an interface. The adapter pattern is primarily used in situations where you want to reuse some existing classes, but the interfaces are inconsistent with the reuse environment requirements.
Want to use an already existing class, but if its interface, that is, its methods and your requirements are not the same, you should consider using the adapter mode.
such as the purchase of third-party development components, the interface is not the same interface with our own system, or for some reason cannot call the component directly, you can consider the adapter.
The UML diagram is as follows:
Figure 1: Class-Mode Adapter
Figure 2: Object Mode adapter
The code is as follows:
Adapter.h
1 #ifndef _adapter_h_ 2 #define _ADAPTER_H_ 3 4//target interface class, customer required interface 5 class target 6 {7 Public:8 target (); 9 vir Tual ~target ();// Define standard interface 11};12 13//The class that needs to be adapted in the category of Adaptee15 {public:17 adaptee (); ~adaptee (); specificrequest (); 20};21 22//Class mode, adapter class, through public inheritance to obtain the effect of interface inheritance, through private inheritance to achieve the effect of inheritance 23 Class Adapter:public target,private Adaptee24 {public:26 Adapter (); ~adapter (); virtual void Request ();//implement target definition of request interface 29};30 31//Object mode, adapter class, Inherit target class, implement Adaptee in combination mode Adapter1:public Target33 {public:35 Adapter1 (adaptee* adaptee), Adapter1 (), Notoginseng ~adapter1 (), + virtual void Request ();//implement target definition of request interface private:40 adaptee* _adaptee;41};42 #endif
Adapter.cpp
1 #include "Adapter.h" 2 #include <iostream> 3 4 using namespace Std; 5 6 Target::target () 7 {} 8 9 Target::~target () {}11 target::request () {cout << target::reques T () "<< endl;15}16 adaptee::adaptee () +}20 Adaptee::~adaptee () () {}24-void adaptee::specificreques T () cout << "Adaptee::specificrequest ()" << endl;28}29 30//Class mode Adapter31 Adapter::adapter () 32 {33 }34 Adapter::~adapter () ()}38 (PNS) adapter::request () $ cout << adapter::request () << Endl ; This->specificrequest () cout << "----------------------------" <<endl;44}45 46//Object mode adapt Er47 adapter1::adapter1 (): _adaptee (New Adaptee),}50 (Adapter1::adapter1 adaptee*) * _adaptee Ptee = _adaptee;54}55 adapter1::~adapter1 () ()}59 () adapter1::request ()/cout adapter1::r () Equest () "<< endl;63 this->_adaptee->specificrequest(); cout << "----------------------------" <<endl;65}
Main.cpp
1 #include "Adapter.h" 2 3 int main () 4 {5 //class mode Adapter 6 target* ptarget = new Adapter (); 7 ptarget-> Request (); 8 9 //Object mode Adapter110 adaptee* ade = new Adaptee (); target* ptarget1= New Adapter1 (ADE); Ptarget1->request (); //Object mode Adapter215 target* pTarget2 = new Adapter1 (); ptarget2-> Request (); + return 0;19}
C + + design mode-adapter adapter mode (RPM)