适配器模式(接口隔离)

#include <iostream>
#include <string>
using namespace std;
//适配器模式:把一个类的接口变换成客户端所期待的另一种接口
//,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。
//适配类可以根据参数返还一个合适的实例给客户端。
class Player
{
public:
	string name;
	Player(string name)
	{
		this->name = name;
	}
	virtual void attack() = 0;
	virtual void defence() = 0;
};

class Forwards :public Player
{
public:
	Forwards(string name) :Player(name){}
	void attack()
	{
		cout << name << " 前锋进攻" << endl;
	}
	void defence()
	{
		cout << name << " 前锋防守" << endl;
	}
};

class Center :public Player
{
public:
	Center(string name) :Player(name){}
	void attack()
	{
		cout << name << " 中锋进攻" << endl;
	}
	void defence()
	{
		cout << name << " 中锋防守" << endl;
	}
};

class Backwards :public Player
{
public:
	Backwards(string name) :Player(name){}
	void attack()
	{
		cout << name << " 后卫进攻" << endl;
	}
	void defence()
	{
		cout << name << " 后卫防守" << endl;
	}
};
/*****************************************************************/
class ForeignCenter
{
public:
	string name;
	ForeignCenter(string name)
	{
		this->name = name;
	}
	void myAttack()
	{
		cout << name << " 外籍中锋进攻" << endl;
	}
	void myDefence()
	{
		cout << name << " 外籍后卫防守" << endl;
	}
};
/*****************************************************************/
class Translator :public Player
{
private:
	ForeignCenter *fc;
public:
	Translator(string name) :Player(name)
	{
		fc = new ForeignCenter(name);
	}
	void attack()
	{
		fc->myAttack();
	}
	void defence()
	{
		fc->myDefence();
	}
};
/*****************************************************************/
int main()
{
	Player *p1 = new Center("李俊宏");
	p1->attack();
	p1->defence();

	Translator *tl = new Translator("姚明");
	tl->attack();
	tl->defence();
	cin.get();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值