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;

}

 

相关文章:

  • 2021-10-19
  • 2021-08-27
  • 2022-01-16
  • 2021-04-06
猜你喜欢
  • 2021-12-16
  • 2022-01-22
  • 2021-06-28
  • 2022-12-23
  • 2021-06-25
相关资源
相似解决方案