Adapt模式

原创
2017/04/25 13:50
阅读数 34
//Adapt模式 

//组合模式
//需要被Adapt的类
class ITarget
{
public:
	ITarget(){}
	virtual~ITarget(){}
public:
	virtual void Request()=0;
};
// 与被Adapt对象提供不兼容接口的类
class Adaptee
{
public:
	Adaptee(){}
	~Adaptee(){}
public:
	void SpecialRequest(){printf("this is a call SpecialRequest\n");};
};

class Adapter: public ITarget
{
public:
	Adapter(Adaptee* pAdaptee){}
    ~Adapter(){}

	virtual void Request()
	{
		m_pAdptee->SpecialRequest();
	}

private:
	Adaptee* m_pAdptee;
};

int _tmain(int argc, _TCHAR* argv[])
{
	//Adapt模式
	Adaptee *pAdaptee=new Adaptee;
	ITarget *padapter=new Adapter(pAdaptee);
	padapter->Request();
	delete pAdaptee;
	delete padapter;
	//Adapt模式
	getchar();
	return 0;
}

//继承模式

//需要被Adapt的类
class ITarget
{
public:
	ITarget(){}
	virtual~ITarget(){}
public:
	virtual void Request()=0;
};
// 与被Adapt对象提供不兼容接口的类
class Adaptee
{
public:
	Adaptee(){}
	virtual~Adaptee(){}
public:
	void SpecialRequest(){printf("this is a call SpecialRequest\n");};
};

class Adapter0: public ITarget,Adaptee
{
public:
	Adapter0(){}
	~Adapter0(){}

	virtual void Request()
	{
		Adaptee::SpecialRequest();
	}
};

//继承模式

int _tmain(int argc, _TCHAR* argv[])
{
	//Adapt模式
	ITarget *padapter=new Adapter0;
	padapter->Request();
	delete padapter;
	//Adapt模式
	getchar();
	return 0;
}

//Adapt模式
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部