C++设计模式(十三)—适配器模式

适配器模式

将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。适配器模式讲了两种类型,类适配器模式和对象适配器模式。
本文使用书中篮球翻译的例子,代码使用C++语言描述,代码存在的不足或问题有望各位指出。
(1)对象适配器模式模版
//对象适配器模式
#include <iostream>
using namespace std;

class Target
{
public:
    Target(){}
    virtual ~Target(){}
    virtual void request()
    {
        cout <<"普通请求!"<<endl;
    }
};

class Adaptee
{
public:
    void specificRequest()
    {
        cout <<"特殊的请求!"<<endl;
    }
};

class Adapter :public Target
{
public:
    Adapter(Adaptee *adaptee)
    {
        this->adaptee = adaptee;
    }
    ~Adapter()
    {
    }
    void request() override
    {
        adaptee->specificRequest();
    }
private:
    Adaptee *adaptee;
};

int main()
{
    Adaptee* adaptee = new Adaptee();
    Target *target=new Adapter(adaptee);
    target->request();
    delete adaptee;
    delete target;
    return 0;

}
(2)类适配器模式模版
//类适配器模式
#include <iostream>
using namespace std;

class Target
{
public:
    Target(){}
    virtual ~Target(){}
    virtual void request()
    {
        cout <<"普通请求!"<<endl;
    }
};

class Adaptee
{
public:
    void specificRequest()
    {
        cout <<"特殊的请求!"<<endl;
    }
};

class Adapter :public Target,private Adaptee
{
public:
    Adapter()
    {
    }
    ~Adapter()
    {
    }
    void request() override
    {
        this->specificRequest();
    }
private:

};



int main()
{

    Target *target=new Adapter();
    target->request();
    delete target;
    return 0;

}
(3)篮球翻译事例使用对象适配器模式实习
#include <iostream>
#include <string>
#include <string>
using namespace std;

class Player
{
public:
    Player(){}
    virtual ~Player(){}
    void setName(string)
    {
        this->name = name;
    }

    virtual void Attack()=0;
    virtual void Defense()=0;
protected:
    string name;

};

class Forwards:public Player
{
public:
    Forwards(){}
    ~Forwards(){}
    Forwards(string name)
    {
        this->name = name;
    }
    void Attack() override
    {
        cout << "前锋 "<< name << " 进攻" << endl;
    }
    void Defense() override
    {
        cout << "前锋 " << name <<" 防守" << endl;
    }

};

class Center: public Player
{
public:
    Center(){}
    ~Center(){}
    Center(string name)
    {
        this->name = name;
    }
    void Attack() override
    {
        cout << "中峰 "<< name << " 进攻" << endl;
    }
    void Defense() override
    {
        cout << "中锋 " << name <<" 防守" << endl;
    }

};

class Guards: public Player
{
public:
    Guards(){}
    ~Guards(){}
    Guards(string name)
    {
        this->name = name;
    }
    void Attack() override
    {
        cout << "后卫 "<< name << " 进攻" << endl;
    }
    void Defense() override
    {
        cout << "后卫 " << name <<" 防守" << endl;
    }

};

class ForeignCenter
{
public:
    ForeignCenter(){}
    ~ForeignCenter(){}
    void setName(string name)
    {
        this->name = name;
    }
    void attack()
    {
        cout <<"外籍中锋 " << name << " 进攻" << endl;
    }
    void defense()
    {
        cout <<"外籍中锋 " << name << " 防守" << endl;
    }
private:
    string name;

};

class Translator: public Player
{
public:
    Translator(){}
    ~Translator()
    {
        delete foreignCenter;
    }
    Translator(string name)
    {
        this->name = name;
        foreignCenter->setName(name);

    }
    void Attack() override
    {
        foreignCenter->attack();
    }
    void Defense() override
    {
        foreignCenter->defense();
    }

private:
    ForeignCenter *foreignCenter = new ForeignCenter();


};

int main()
{
    Player *b = new Forwards("巴蒂尔");
    b->Attack();

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

    Player *ym = new Translator("姚明");
    ym->Attack();
    ym->Defense();
    return 0;

}
使用适配器模式

系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThomasKUI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值