ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++设计模式——适配器模式

2022-02-22 12:02:21  阅读:207  来源: 互联网

标签:ym name 适配器 C++ Player Attack 接口 设计模式 void


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

角色:

(1)Target:这是客户所期待的接口,Target可以是具体的或抽象的类,也可以是接口。

(2)Adaptee:需要适配的类。

(3)Adapter:通过在内部包装一个Adaptee对象,把源接口转换成目标接口。

 

 

 

#include <iostream>
using namespace std;
//球员
class Player
{
protected:
    string name;
public:
    Player(string name):name(name){}
    virtual void Attack() = 0;
    virtual void Defense() = 0;
    virtual~Player() {}
};
//前锋
class Forwards : public Player
{
public:
    Forwards(string name):Player(name){}
    virtual void Attack() {
        cout << "前锋" << name << "进攻" << endl;
    }
    virtual void Defense() {
        cout << "前锋" << name << "防守" << endl;
    }
};

//中锋
class Center : public Player
{
public:
    Center(string name) :Player(name) {}
    virtual void Attack() {
        cout << "中锋" << name << "进攻" << endl;
    }
    virtual void Defense() {
        cout << "中锋" << name << "防守" << endl;
    }
};

//后卫
class Guards : public Player
{
public:
    Guards(string name) :Player(name) {}
    virtual void Attack() {
        cout << "后卫" << name << "进攻" << endl;
    }
    virtual void Defense() {
        cout << "后卫" << name << "防守" << endl;
    }
};

class ForeignCenter {
public:
    void SetName(string name) {
        this->name = name;
    }
    string GstName() {
        return name;
    }
    void ForeignAttack() {
        cout << "外籍中锋 " << name << " 攻击" << endl;
    }
    void ForeignDefense() {
        cout << "外籍中锋 " << name << " 防守" << endl;
    }

private:
    string name;
};

//适配器类 翻译者
class Translator :public Player {
public:
    Translator(string name) :Player(name) {
        ym = new ForeignCenter;
        ym->SetName(name);
    }
    ~Translator()
    {
        if (ym != NULL) delete ym;
    }
    void Attack() { //翻译者将Attack 翻译成 ForeignAttack
        ym->ForeignAttack();
    }
    void Defense() {
        ym->ForeignDefense(); //翻译者将Defense 翻译成 ForeignDefense
    }

private:
    ForeignCenter* ym; //外籍中锋
};
int main()
{
    Player* b = new Forwards("巴蒂尔");
    b->Attack();
    Player* c = new Guards("麦克格雷迪");
    c->Attack();
    Player* ym  = new Translator("姚明"); //姚明问: "Attack和Defense是什么意思?"
    ym->Attack();
    ym->Defense();
    system("pause");
    return 0;
}

使用场景:

什么时候用?

(1)在想使用一个已存在的类,但是如果他的接口,也就是它的方法和你的要求不相同时,就应该考虑用适配器模式。

(2)用了适配器模式,客户代码可以统一调用统一接口就行了,这样可以更简单,更直接,更紧凑。

(3)要在双方都不太容易修改的时候再使用适配器模式适配,而不是一有不同是就使用它。

 

标签:ym,name,适配器,C++,Player,Attack,接口,设计模式,void
来源: https://www.cnblogs.com/Galesaur-wcy/p/15922506.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有