Operation 基本クラスを作成し、それを 2 回派生させて Insert クラスと Erase クラスを作成できます。その後、Operation クラスへのポインターを格納できます。
class Settings
{
public:
bool Insert(std::string const& key, std::string const& value)
{
m_operations.push_back(new Insert(key, value));
return true; //?
}
bool Erase(std::string const& key)
{
m_operations.push_back(new Erase(key));
return true; //?
}
bool Commit()
{
// You know what to do by now right ?
}
private:
class Operation
{
public:
virtual void Execute() = 0
}
class Insert : public Operation
{
public:
Insert(std::string const& key, std::string const& value) :
m_key(key), m_value(value) {}
void Execute() {...}
private:
std::string m_key;
std::string m_value;
}
class Erase : public Operation
{
public:
Erase(std::string const& key) : m_key(key) {}
void Execute() {...}
private:
std::string m_key;
}
std::queue<Operation*> m_operations;
std::map<std::string, std::string> m_settings;
}
次に、操作クラスを設定クラスのフレンドにするか、操作が適用されるマップに参照を渡します。