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

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

  • 相关阅读:
    字符串的长度 -- js
    导入drupal中文语言包
    ubuntu修改iP地址
    人生需要苦难和敌人
    Drupal.theme未解之谜
    如何定义带下标的js数组
    smtp admin email 似乎可以考虑在
    js中的apply 和 call
    js 点号 中括号
    代码调试
  • 原文地址:https://www.cnblogs.com/pzqu/p/9457647.html
Copyright © 2011-2022 走看看