• 适配器模式


    1】什么是适配器模式?
    
    将一个类的接口转换成客户希望的另外一个接口。
    
    Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作。
    
    【2】适配器模式的代码示例:
    
    代码示例如下1:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Adaptee
    {
    public:
        virtual void myRequest()
        {
            cout << "实际上的接口" << endl;
        }
    };
    
    class Target
    {
    public:
        virtual void request() = 0;
        virtual ~Target(){}
    };
    
    class Adapter : public Target
    {
    private:
        Adaptee adaptee;
    public:
        void request()
        {
            adaptee.myRequest();
        }
    };
    
    int main()
    {
        Target *target = new Adapter();
        target->request();
        delete target;
        return 0;
    }
    //Result:
    //实际上的接口
    代码示例如下2:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Player
    {
    public:
        string name;
        Player(string name)
        {
            this->name = name;
        }
        virtual void attack() = 0;
        virtual void defence() = 0;
    }; 
    
    class Forwards : public Player
    {
    public:
        Forwards(string name) : Player(name){}
        void attack()
        {
            cout << name << "前锋进攻" << endl;
        }
        void defence()
        {
            cout << name << "前锋防守" << endl;
        }
    };
    
    class Center : public Player
    {
    public:
        Center(string name) : Player(name){}
        void attack()
        {
            cout << name << "中锋进攻" << endl;
        }
        void defence()
        {
            cout << name << "中锋防守" << endl;
        }
    };
    
    class Backwards : public Player
    {
    public:
        Backwards(string name) : Player(name){}
        void attack()
        {
            cout << name << "后卫进攻" << endl;
        }
        void defence()
        {
            cout << name << "后卫防守" << endl;
        }
    };
    /*****************************************************************/
    class ForeignCenter
    {
    public:
        string name;
        ForeignCenter(string name)
        {
            this->name = name;
        }
        void myAttack()
        {
            cout << name << "外籍中锋进攻" << endl;
        }
        void myDefence()
        {
            cout << name << "外籍中锋防守" << endl;
        }
    };
    /*****************************************************************/
    class Translator : public Player
    {
    private:
        ForeignCenter *fc;
    public:
        Translator(string name) : Player(name)
        {
            fc = new ForeignCenter(name); 
        }
        void attack()
        {
            fc->myAttack();
        }
        void defence()
        {
            fc->myDefence();
        }
    };
    /*****************************************************************/
    int main()
    {
        Player *p1 = new Center("李俊宏");
        p1->attack();
        p1->defence();
        
        Translator *tl = new Translator("姚明");
        tl->attack();
        tl->defence();
        
        return 0;
    }
    //Result:
    /*
    李俊宏中锋进攻
    李俊宏中锋防守
    姚明外籍中锋进攻
    姚明外籍中锋防守
    */

    http://www.cnblogs.com/Braveliu/p/3946831.html

  • 相关阅读:
    半年时间
    deep learning书的阅读
    wgan pytorch,pyvision, py-faster-rcnn等的安装使用
    caffe新版本的各种软件
    你会允许自己家孩子一直不停跟人要东西吗?
    sup inf max min
    leangoo大讲堂—北京站
    使用Leangoo玩转故事地图
    Leangoo:用敏捷开发管理思维做团队协作的SaaS软件
    张江男的逆袭,我如何使用leangoo提升团队效率
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4534593.html
Copyright © 2020-2023  润新知