设计模式--适配器模式

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;


//适配器模式  将已经写好的接口  转化成目标需要的接口
struct MyPrint
{
	void operator()(int v1, int v2) {
		cout << v1 + v2 << endl;
	}
};

//定义目标接口  适配成什么样的
class Target {
public:
	virtual void operator()(int v) = 0;
};

//写适配器
class Adapter :public Target {
public:
	Adapter(int nArg)
	{
		this->param = nArg;
	}
	virtual void operator()(int v)
	{
		print(v, param);
	}

public:
	int param;
	MyPrint print;
};

Adapter MyBind2nd(int v)
{
	return Adapter(v);
}


int main()
{
	vector<int> v;
	for (int i = 0; i < 5; i++)
	{
		v.push_back(i);
	}

	for_each(v.begin(), v.end(), MyBind2nd(5));



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