• 适配器模式


    前序

    姚明,大家都认识吧。在他刚去NBA的时候,什么都听不懂,必须在旁边配个翻译,否则就无法听懂教练在说什么。这也正符合了设计模式中的一种模式:适配器模式。

    适配器模式

    将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

    实现方式(UML类图)


    实现代码

    #include <stdio.h>

     

    // 球员

    class Player

    {

    public:

           Player(char* _name) : name(_name){}

          

           virtual void Attack()=0;

           virtual void Defense()=0;

     

           char* name;

    };

     

    // 前锋

    class Forwards : public Player

    {

    public:

           Forwards(char* name) : Player(name){}

          

           virtual void Attack()

           {

                  printf("前锋 %s 进攻 ", name);

           }

          

           virtual void Defense()

           {

                  printf("前锋 %s 防守 ", name);

           }

    };

     

    // 中锋

    class Center : public Player

    {

    public:

           Center(char* name) : Player(name){}

          

           virtual void Attack()

           {

                  printf("中锋 %s 进攻 ", name);

           }

          

           virtual void Defense()

           {

                  printf("中锋 %s 防守 ", name);

           }

    };

     

    // 后卫

    class Guards : public Player

    {

    public:

           Guards(char* name) : Player(name){}

          

           virtual void Attack()

           {

                  printf("后卫 %s 进攻 ", name);

           }

          

           virtual void Defense()

           {

                  printf("后卫 %s 防守 ", name);

           }

    };

     

    // 外籍中锋

    class ForeignCenter

    {

    public:

           void Attack()

           {

                  printf("外籍中锋 %s 进攻 ", name);

           }

          

           void Defense()

           {

                  printf("外籍中锋 %s 防守 ", name);

           }

          

           char* name;

    };

     

    // 翻译者

    class Translator : public Player

    {

    public:

           Translator(char* name) : Player(name)

           {

                  wjzf.name = name;

           }

          

           virtual void Attack()

           {

                  wjzf.Attack();

           }

          

           virtual void Defense()

           {

                  wjzf.Defense();

           }

    protected:

           ForeignCenter wjzf;

    };

     

    int main()

    {

           Player* b = new Forwards("巴蒂尔");

           b->Attack();      

           Player* m = new Guards("麦克格雷迪");

           m->Attack();      

           Player* ym = new Translator("姚明");

           ym->Attack();

           ym->Defense();      

           delete b;

           delete m;

           delete ym;      

           return 0;

    }

    运行结果

  • 相关阅读:
    BZOJ3149 CTSC2013 复原 搜索
    BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥
    THUWC2019-1:Reach out
    Luogu4630 APIO2018 Duathlon 圆方树、树形DP
    Luogu4606 SDOI2018 战略游戏 圆方树、虚树、链并
    BZOJ3720 Gty的妹子树 询问分块、主席树
    CF809E Surprise me! 莫比乌斯反演、虚树
    LOJ2542 PKUWC2018 随机游走 min-max容斥、树上高斯消元、高维前缀和、期望
    LOJ2541 PKUWC2018 猎人杀 期望、容斥、生成函数、分治FFT
    CF797F Mice and Holes 贪心、栈维护DP
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4364020.html
Copyright © 2020-2023  润新知