A_UpdateHandler(Control &control, const int threshold)
void Update(Graphic *graphic) const
// want to pass this in as it doesn’t make
// sense for all the handler objects to
// store the same pointer
bool value = m_Control.GetCurrentValue() > m_Threshold;
const int m_Threshold;
Next I needed to create a collection of these handlers and initialise them. So a couple of (private) additions to class B did the trick.
// Note: using a for-loop here to illustrate the current
// point more clearly, I’ll leave refactoring to a pref-
// erable implementation using for_each as an exercise
// for the reader! Clue: try using std::transform with
// a back_inserter.
// When I first came across this problem an Init member
// already existed in the real version of “class B”
// that iterated through the collection anyway,
// perhaps I should go back and refactor that too…
for(APtrContainer::iterator it = m_APCollection.begin();
it!= m_APCollection.end(); ++it)
A &a = **it;
std::string &name = a.GetName();
control = GetControlByName(name);
const int threshold = a.GetThreshold();
Now class B’s update function can now be written using a call to std::for_each…
// we have a problem!
Another problem!! Oh no, but before we explore what it is an how I overcame it to get to the end of this refactor (finally!!) I’d like to point out the use of std::mem_fun_ref above. It does the same as std::mem_fun but works with a collection of references instead of pointers.
template<class Result, class Type>
So, the problem with calling A_UpdateHandler::Update in this way is that there’s no way of passing the Graphic * parameter that we need - or is there? Enter that old favourite, std::bind2nd and the final update function looks like this:
Graphic &graphic = GetGraphic();
Which in terms of sheer aethestetics looks a lot prettier than our original for-loop! This isn’t mere triviality, in my experience elegant looking code usually indicates a more elegantly designed solution under the hood. There are several reasons why this is the case here.
... and I'll go through it with you in Part IV :)