Hello!
In couple of recent projects that I took part in I was almost addicted to the following coding pattern: (I'm not sure if there is a proper name for this, but anyway...)
Let's say some object is in some determined state and we wan't to change this state from outside. These changes could mean any behaviour, could invoke any algorithms, but the fact is that they focus on changing the state (member state, data state, etc...) of some object.
And let's call one discrete way of changing those object a Mutator
. Mutators
are applied once (generally) and they have some internal method like apply(Target& target, ...)
, which instantly provokes changing the state of the object (in fact, they're some sort of functional objects).
They also could be easily assimilated into chains and applied one-by-one (Mutator m1, m2, ...
); they also could derive from some basic BasicMutator
with virtual void apply(...)
method.
I have introduced classes called InnerMutator
and ExplicitMutator
which differ in terms of access - first of them can also change the internal state of the object and should be declared as a friend (friend InnerMutator::access;
).
In those projects my logic turned to work the following way:
- Prepare available mutators, choose which to apply
- Create and set the
object
to some determined state foreach (mutator) mutator.apply(object);
Now the question.
This scheme turnes to work well and (to me) seems as a sample of some non-standard but useful design pattern.
What makes me feel uncomfortable is that
InnerMutator
stuff. I don't think declaring mutator a friend to every object which state could be changed is a good idea and I wan't to find the appropriate alternative.Could this situation be solved in terms of
Mutators
or could you advice some alternative pattern with the same result?
Thanks.