Adapter mode (Adapter): Convert the interface of a class into another interface that the customer wants. The Adapter pattern enables classes that would otherwise not work together due to incompatible interfaces to work together.
When to use the Adapter pattern:
It is needed when two classes do the same or similar things, but have different interfaces.
Use the adapter mode when it is not easy for both parties to modify.
Pattern implementation:
[code]//Target class Target{ public: virtual void Request(){ std::cout << "Target::Request\n"; } }; //Adaptee适配(者)的类 class Adaptee{ public: void SpecificRequest(){ std::cout << "Adaptee::SpecificRequest\n"; } }; //Adapter,适配器 class Adapter: public Target, Adaptee{ public: void Request(){ Adaptee::SpecificRequest(); } };
Client:
[code]//Client int main(){ Target *targetObj = new Adapter(); targetObj->Request(); //Output: Adaptee::SpecificRequest delete targetObj; targetObj = NULL; return 0; }
The above is the content of the C++ design pattern brief introduction to the adapter pattern. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!