十三、適配器模式

適配器模式(Adapter)將一個類的接口轉換成客戶希望的另外一個接口。Adapter模式使得原本由於接口不兼容而不能一起工作的哪些類可以一起工作。

//Adapter.h
#ifndef _ADAPTER_H_
#define _ADAPTER_H_
#include <iostream>
using namespace std;
class Adaptee;
class Tatget
{
public:
	virtual void Request()
	{
		cout<<"common request..."<<endl;
	}
};
class Adapter:public Tatget
{
public:
	Adapter()
	{
		_ape = new Adaptee();
	}
	void Request()
	{
		_ape->SpecificRequest();
	}
private:
	Adaptee* _ape;
};
#endif

//Adaptee.h
#ifndef _ADAPTEE_H_
#define _ADAPTEE_H_
#include <iostream>
using namespace std;
class Adaptee
{
public:
	void SpecificRequest()
	{
		cout<<"Specific Request..."<<endl;
	}
};
#endif

//main.cpp
#include "Adaptee.h"
#include "Adapter.h"

int main()
{
	Tatget* tg = new Adapter();
	tg->Request();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章