
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
//Target
class CText {
//Request()
virtual string getText() = 0;
};
//Adaptee
class StringList {
vector<string> rows = {};
public:
//SpecificRequest()
string getString() {
stringstream ss;
for(int i = 0; i < rows.size(); ++i)
{
if (i != 0) ss << endl;
ss << rows[i];
}
return ss.str();
}
void add(string value) {
rows.push_back(value);
}
};
//Adapter
class TextAdapter: CText {
public:
StringList rowList;
//Request()
string getText() {
return rowList.getString();
}
};
//Client
TextAdapter adapter;
adapter.rowList.add("line 1");
adapter.rowList.add("line 2");
string text = adapter.getText();
//text: line 1
// line 2
cout << "text:" << endl << text;
| Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. |