#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;
}
設計模式--適配器模式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.