c++设计模式:适配器模式
1.主要思想:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
对于不同的客户我们可以这样使用适配器模式。
2.实现方法:
#include<iostream> using namespace std; // "ITarget" class Target { public: // Methods virtual void Request(){}; }; // "Adaptee" class Adaptee { public: // Methods void SpecificRequest() { cout<<"Called SpecificRequest()"<<endl; } }; // "Adapter" class Adapter : public Adaptee, public Target { public: // Implements ITarget interface void Request() { // Possibly do some data manipulation // and then call SpecificRequest this->SpecificRequest(); } }; int main() { // Create adapter and place a request Target *t = new Adapter(); t->Request(); return 0; }