I have a class representing the state of my application and I want to create many commands to update this state.
class State{
private:
list<int> intList;
...
}
class ICommand {
public:
virtual ~ICommand(){}
virtual void execute(State* state) = 0;
}
ICommand
implementations will specify how to update the state
, so they should interact with its private fields.
My problem is: what is the best approach in C++ to let commands update the private field of State
?
If I expose intList
with a public getter I can't guarantee that only commands update the state.
If I would use class friendship
I have to link State
with command implementation because linking it with the interface does not work and it seems to me not so good...
Is my doubt about 'friendship' unfounded? Should I choose a different approach?