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;

    }

    运行结果

  • 相关阅读:
    关于TextField
    判断一个显示对象是否移除
    不争气的Discuz!NT 3.6和MVC3整合,主要实现同步登录和注册,登出。
    我的博客是英文的
    TFS不提供 Team Foundation 服务的解决办法。
    四 为提高entity framework 性能,要注意哪些事情.
    三 EF 和ado.net 的性能对比.
    一 关于大项目的经验总结
    在.net 中,ajax 如何调用本页数据源
    关于有序guid 的使用
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4364020.html
Copyright © 2011-2022 走看看