zoukankan      html  css  js  c++  java
  • 适配器模式

    #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;
    }
    

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

  • 相关阅读:
    音频处理入门笔记
    python对象-多态
    python对象的不同参数集合
    python多重继承的钻石问题
    python对象的多重继承
    python类继承的重写和super
    Python继承扩展内置类
    python对象继承
    Python模块与包
    Pyhton对象解释
  • 原文地址:https://www.cnblogs.com/pzqu/p/9457647.html
Copyright © 2011-2022 走看看