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

    前序

    姚明,大家都认识吧。在他刚去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;

    }

    运行结果

  • 相关阅读:
    关于Table.Compute("Sum(qty)","XXX")的坑
    转载:window.open传值
    一些有用的SQL语句
    VS调试的时候也会锁定SQL对象
    关于数据中时间的坑
    那些年踩过的坑
    一段SQL
    关于下载的问题
    download excle的幾個問題的解決
    《浪潮之巅》读书笔记——第10章 惠普
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4364020.html
Copyright © 2011-2022 走看看