设计模式学习--适配器模式(类适配器和对象适配器)

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

适配器的用法

  在想使用一个已经存在的类,但它的接口,也就是它的方法和要求不相同时,就应该考虑使用适配器模式。两个类所做的事情相同或相似,但是具有不同的接口时需要使用它。双方都不太容易修改的时候可以使用适配器模式。

1、类适配器


  从图中可以看出Adaptee没有Request函数,但如果客户端需要使用这个函数的话,可以创建一个Adapter类,同时继承两个类,在Adapter类中,创建Request函数来调用SpeclificRequest函数。

#include <iostream>

using namespace std;

class Player													//运动员抽象类
{
	string m_name;												//运动员姓名
public:
	Player(string name):m_name(name){};							//姓名赋值
	string getname(){return m_name;}							//获取姓名
	virtual void Attack() = 0;									//进攻纯虚函数
	virtual void Defense() = 0;									//防守纯虚函数
};

class Forward : public Player									//前锋类
{
public:
	Forward(string name):Player(name){};						//姓名赋值
	void Attack()												//进攻函数
	{
		cout<<"前锋 "<<this->getname()<<" 进攻"<<endl;
	}
	
	void Defense()												//防守函数
	{
		cout<<"前锋 "<<this->getname()<<" 防守"<<endl;		
	}
};

class Guard : public Player										//后卫类
{
public:
	Guard(string name):Player(name){};							//姓名赋值
	void Attack()												//进攻函数
	{
		cout<<"后卫 "<<this->getname()<<" 进攻"<<endl;
	}
	
	void Defense()												//防守函数
	{
		cout<<"后卫 "<<this->getname()<<" 防守"<<endl;
	}
};

class ForeignCenter												//外籍中锋
{
	string m_name;
public:
	void Setname(string name){m_name = name;}					//姓名赋值
	string Getname(){return m_name;}							//获取姓名
	
	void jinggong()												//外语进攻
	{
		cout<<"中锋 "<<this->Getname()<<" 进攻"<<endl;		
	}
	
	void fangshou()												//外语防守
	{
		cout<<"中锋 "<<this->Getname()<<" 防守"<<endl;		
	}
};

class Translator : public Player, public ForeignCenter			//外籍中锋的翻译  继承运动员类和外籍中锋类
{	
public:
	Translator(string name):Player(name)						//构造函数  创建外籍中锋实例
	{
		this->Setname(name);
	}
	
	void Attack()												//进攻函数
	{
		this->jinggong();
	}
	void Defense()												//防守函数
	{
		this->fangshou();
	}
};

int main()
{
	Player* b = new Forward("巴蒂尔");
	b->Attack();
	
	Player* m = new Guard("麦克格雷迪");
	m->Attack();
	
	Player* ym = new Translator("姚明");
	ym->Attack();
	ym->Defense();
	
	return 0;
}


2、对象适配器


  从图中可以看出类适配器和对象适配器的功能其实差不多,只是在适配的过程中,类适配器是使用多重继承来调用两个函数,而对象适配器是通过对象来调用。

#include <iostream>

using namespace std;

class Player													//运动员抽象类
{
	string m_name;												//运动员姓名
public:
	Player(string name):m_name(name){};							//姓名赋值
	string getname(){return m_name;}							//获取姓名
	virtual void Attack() = 0;									//进攻纯虚函数
	virtual void Defense() = 0;									//防守纯虚函数
};

class Forward : public Player									//前锋类
{
public:
	Forward(string name):Player(name){};						//姓名赋值
	void Attack()												//进攻函数
	{
		cout<<"前锋 "<<this->getname()<<" 进攻"<<endl;
	}
	
	void Defense()												//防守函数
	{
		cout<<"前锋 "<<this->getname()<<" 防守"<<endl;		
	}
};

class Guard : public Player										//后卫类
{
public:
	Guard(string name):Player(name){};							//姓名赋值
	void Attack()												//进攻函数
	{
		cout<<"后卫 "<<this->getname()<<" 进攻"<<endl;
	}
	
	void Defense()												//防守函数
	{
		cout<<"后卫 "<<this->getname()<<" 防守"<<endl;
	}
};

class ForeignCenter												//外籍中锋
{
	string m_name;
public:
	void setname(string name){m_name = name;}					//姓名赋值
	string getname(){return m_name;}							//获取姓名
	
	void jinggong()												//外语进攻
	{
		cout<<"中锋 "<<this->getname()<<" 进攻"<<endl;		
	}
	
	void fangshou()												//外语防守
	{
		cout<<"中锋 "<<this->getname()<<" 防守"<<endl;		
	}
};

class Translator : public Player								//外籍中锋的翻译
{
	ForeignCenter* center;
public:
	Translator(string name):Player(name)						//构造函数  创建外籍中锋实例
	{
		center = new ForeignCenter();
		center->setname(name);
	}
	void Attack()												//翻译进攻信号
	{
		center->jinggong();
	}
	
	void Defense()												//翻译防守信号
	{
		center->fangshou();
	}
};

int main()
{
	Player* b = new Forward("巴蒂尔");
	b->Attack();
	
	Player* m = new Guard("麦克格雷迪");
	m->Attack();
	
	Player* ym = new Translator("姚明");
	ym->Attack();
	ym->Defense();
	
	return 0;
}


显示结果:

[hjf@hjf 设计模式]$ ./a.out 
前锋 巴蒂尔 进攻
后卫 麦克格雷迪 进攻
中锋 姚明 进攻
中锋 姚明 防守

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值