大话设计模式C++实现-第17章-适配器模式

一、UML图


关键词:Client须要Request()函数,Adaptee提供的是SpecificRequest()函数,Adapter提供一个Request()函数将Adaptee和SpecificeRequest()函数封装起来。


二、概念

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


三、说明

角色:

(1)Target:这是客户所期待的接口,Target能够是详细的或抽象的类,也能够是接口。

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

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

什么时候用?

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

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

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


四、C++实现

(1)Adapter.h

#ifndef ADAPTER_H
#define ADAPTER_H

#include <string>
#include <iostream>

//Target,此处为运动员
class Player
{
protected:
	std::string name;
public:
	Player(std::string name)
	{
		this->name=name;
	}
	virtual void Attack()=0;
	virtual void Defense()=0;
};

//Adaptee,此处为外籍中锋。它的接口和Target的接口不一样,需要翻译来帮忙转换
class ForeignCenter
{
private:
	std::string name;
public:
	void SetName(std::string name)
	{
		this->name=name;
	}
	std::string GetName()
	{
		return name;
	}
	void ForeignAttack()
	{
		std::cout<<"外籍中锋  "<<name<<"  进攻"<<std::endl;
	}

	void ForeignDefense()
	{
		std::cout<<"外籍中锋  "<<name<<"  防守"<<std::endl;
	}
};

//Adapter,此处为翻译
class Translator:public Player
{
private:
	ForeignCenter* wjzf;
public:
	Translator(std::string name):Player(name)
	{
		wjzf=new ForeignCenter;
		wjzf->SetName(name);
	}
	~Translator()
	{
		delete wjzf;
	}
	void Attack()
	{
		wjzf->ForeignAttack();
	}

	void Defense()
	{
		wjzf->ForeignDefense();
	}
};


//以下是普通的  接口和Target接口一样的 3个子类,必需要翻译
//前锋
class Forwards:public Player
{
public:
	Forwards(std::string name):Player(name){}

	void Attack()
	{
		std::cout<<"前锋  "<<name<<"  进攻"<<std::endl;
	}

	void Defense()
	{
		std::cout<<"前锋  "<<name<<"  防守"<<std::endl;
	}
};

//中锋
class Center:public Player
{
public:
	Center(std::string name):Player(name){}

	void Attack()
	{
		std::cout<<"中锋  "<<name<<"  进攻"<<std::endl;
	}

	void Defense()
	{
		std::cout<<"中锋  "<<name<<"  防守"<<std::endl;
	}
};

//后卫
class Guards:public Player
{
public:
	Guards(std::string name):Player(name){}

	void Attack()
	{
		std::cout<<"后卫  "<<name<<"  进攻"<<std::endl;
	}

	void Defense()
	{
		std::cout<<"后卫  "<<name<<"  防守"<<std::endl;
	}
};




#endif


(2)Client.cpp

#include "Adapter.h"
#include <iostream>
#include <cstdlib>
#include <string>

//Client
void 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;

	system("pause");
}


(3)执行截图



原文地址:https://www.cnblogs.com/gavanwanggw/p/7078062.html