Re: Interfaces and concepts
Hello,
The question is, should DoSomethingHelper be an interface (with a pure
virtual assist() method) or a template (where DoSomethingHelper is a
stand-in for anything with an assist() method)? Of course, there are
probably cases where you are forced to use one or the other (CRTP or
type erasure scenarios, I'd imagine), but what about the general case?
technically, my copy of "Design Patterns: Elements of ..." speaks of the
Template Method's intent as "define the skeleton of an algorithm in an
operation, deferring some steps to _subclasses_". So if we're being
purists about it, using a C++ template does not quite fit the Template
Method pattern, even though someone you talk to about it might be able
to understand what you mean.
When using C++ templates, a better fit for describing the design pattern
might be Strategy (or Policy, as it is better known in the C++
community): "define a family of algorithms, encapsulate each one, and
make them interchangeable. Strategy lets the algorithm vary
independently from clients that use it".
Suppose you have some kind of template method design pattern going on,
like so:
DoSomethingHelper helper;
helper.assist();
Please note that that's not quite the gist of the Template Method. The
Template Method in this case would look something like this:
class DoSomethingHelper {
public:
void assist();
virtual ~DoSomethingHelper();
protected:
virtual void assistOperation1() = 0;
virtual void assistOperation2() = 0;
};
void DoSomethingHelper::assist()
{
... do things
assistOperation1();
... do other things
assistOperation2();
... yet more things
}
Simply having a pure virtual assist() member function that you override
in derived classes does not constitute a design pattern as such in C++.
Hope this helps,
Razvan Cojocaru
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]