有一个已经写好的接口,但是不符合需求,适配器就是将已有的接口转化为需要的形式
class MyPrint{
public:
void operator()(int a, int b){
cout << a + b << endl;
}
};
class TargetInterface{
public:
virtual void operator()(int v) = 0;
};
class MyAdaptor : public TargetInterface{
public:
MyAdaptor(int param):param(param){}
void operator()(int v){
print(v, param);
}
private:
MyPrint print;
int param;
};
MyAdaptor myBind2nd(int v){
return MyAdaptor(v);
}
int main()
{
vector<int> v;
for(int i = 0; i < 10; ++i){
v.push_back(i);
}
for_each(v.begin(), v.end(), myBind2nd(10));
return 0;
}