views:

126

answers:

2

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:

  1. Prepare available mutators, choose which to apply
  2. Create and set the object to some determined state
  3. 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.

+3  A: 

Would it not be better to simply make those Mutators methods of the classes they're changing? If there's common functionality you want several classes to have then why not make them inherit from the same base class? Doing this will deal with all the inner mutator discomfort I would imagine.

If you want to queue up changes you could store them as sets of function calls to the methods within the classes.

Jon Cage
Well, my scheme generally benefits in case when you want to add different types of `mutators` after and define them outside of your `object` (they could be *stream filters*, for example). I assume that if I take the way you've describe, I would have to alter the class itself and looks like it's not the appropriate solution.
Kotti
Actually, that was the reason I had to add all that `mutator` stuff into my project :)
Kotti
What's stopping you from going back and adding the new functionality? Surely you'll end up with a neater solution if all of a classes functions which modify it's internals are kept with the class itself?
Jon Cage
+1 Have to agree with Jon here. You still have to modify your original classes to make your InternalMutators into friends. If you're going to bother doing that, might as well provide methods to reliably change your internals in the original classes that provide them. Even if I disagree with your mutator design, I think it's at least a great deal better if all your mutator classes only used the public methods provided by the objects they mutate. Otherwise you have tight data coupling in places where you could have avoided it.
+5  A: 

I hope this isn't taken offensively, but I can't help but think this design is an overly fancy solution for a rather simple problem.

Why do you need to aggregate mutators, for instance? One can merely write:

template <class T>
void mutate(T& t)
{
    t.mutate1(...);
    t.mutate2(...);
    t.mutate3(...);
}

There's your aggregate mutator, only it's a simple function template relying on static polymorphism rather than a combined list of mutators making one super-mutator.

I once did something that might have been rather similar. I made function objects which could be combined by operator && into super function objects that applied both operands (in the order from left to right) so that one could write code like:

foreach(v.begin(), v.end(), attack && burn && drain);

It was really neat and seemed really clever to me at the time and it was pretty efficient because it generated new function objects on the fly (no runtime mechanisms involved), but when my co-workers saw it, they thought I was insane. In retrospect I was trying to cram everything into functional style programming which has its usefulness but probably shouldn't be over-relied on in C++. It would have been easy enough to write:

BOOST_FOR_EACH(Something& something, v)
{
    attack(something);
    burn(something);
    drain(something);
}

Granted it is more lines of code but it has the benefit of being centralized and easy to debug and doesn't introduce alien concepts to other people working with my code. I've found it much more beneficial instead to focus on tight logic rather than trying to forcefully reduce lines of code.

I recommend you think deeply about this and really consider if it's worth it to continue with this mutator-based design no matter how clever and tight it is. If other people can't quickly understand it, unless you have the authority of boost authors, it's going to be hard to convince people to like it.

As for InnerMutator, I think you should avoid it like the plague. Having external mutator classes which can modify a class's internals as directly as they can here defeats a lot of the purpose of having internals at all.

+1: My thoughts exactly on the inner mutator points!
Jon Cage