适配器模式

#include <stdlib.h>
#include <malloc.h>
#include <iostream>
using namespace std;
class Base{
    public:
    virtual void    process(){

        }
};
class Adaptee{
    public:
    virtual void Request(){
            cout<<"get"<<endl;
        }
};
class Derived:public Base{
    public:
    void    process(){

        }
};
class Adapter:public Base{
    private:
        Adaptee *p;
    public:
      Adapter(Adaptee *p){
        this->p=p;
      }
        virtual void process(){
/*我们必须在这里区分代理模式,区别于适配器可以在这里实现功能*/
            p->Request();
        }

};

int main(void)
{
    Base *d=new Derived();
    Adaptee *A=new Adaptee();
    Base *a=new Adapter(A);
    a->process();
    return 0;
}

我们的适配器就像一个翻译,在你需要的时候一直站在你的旁边默默地翻译,不需要继承。
确切的说,我们在需要使用一个类的时候,假如此时此刻我们因为它的接口和我们的要求不一样,就应该使用适配器模式,我们就可以构造一个新的类来统一接口。
可以称为亡羊补牢的方法

原文地址:https://www.cnblogs.com/pzqu/p/9457647.html