Re: multiple interfaces using template method pattern
I figured out one possible way to do it. The Widget class is same as
before except I've added a protected DoUpdate() virtual method.
class Widget
{
public:
int Process( Gadget& );
bool IsDone();
private:
virtual int DoProcessPhase1( Gadget& );
virtual int DoProcessPhase2( Gadget& );
virtual bool DoIsDone();
//NEW METHOD(S) FOR SYSTEM TO USE INDIRECTLY
protected:
virtual int DoUpdate();
};
//2nd interface for system to use which also uses the template method
pattern design
class Updateable
{
public:
void Update();//calls DoUpdate
private:
virtual void DoUpdate()=0;
};
//new class to glue any class to an Updateable and route calls to it
template<class T>
class UpdateRouter : public T, public Updateable
{
private:
void DoUpdate(){ T::DoUpdate(); }//T(the base class here) must
provide DoUpdate
};
//...inside my widget factory...
UpdateRouter<Widget>* router = new UpdateRouter<Widget>;
Widget* widget = dynamic_cast<Widget*>(router);//this returned to
client
Updateable* update = dynamic_cast<Updateable*>(router);//this given to
system
After all this, 'friends' are starting to look at least a whole lot
simpler. Any comment on this technique?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]