Archived

This topic is now archived and is closed to further replies.

antareus

Criticism of the Template method

Recommended Posts

I just looked it up after someone linked to an article on it. Don''t quite agree with the CUJ saying it should be used everywhere public virtual functions would be. Quite frankly in some classes (interfaces for one) it seems like pure overkill and is basically two functions for every one. In a bigger class it seems ridiculous. Also the standard library doesn''t use it everywhere.

Share this post


Link to post
Share on other sites
How can it possibly be 2 functions instead of one? Instead of determining the function at runtime via a function pointer, the location of the function is determined at compile time, removing the dereference of the virtual function pointer.

About the only time I can think of skipping it is when executable size is an issue, and speed isn''t.

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
How can it possibly be 2 functions instead of one? Instead of determining the function at runtime via a function pointer, the location of the function is determined at compile time, removing the dereference of the virtual function pointer.

About the only time I can think of skipping it is when executable size is an issue, and speed isn''t.



class C
{
public:
virtual ~C();
void operation()
{
doOperation();
}

protected:
virtual void doOperation() = 0;
};

Seems kind of stupid when all it does is call doOperation(). Yeah, I know, I can customize behavior of the derived classes by tampering with the base class'' implementation, but in some places it doesn''t make any sense at all, like a socket class.

Share this post


Link to post
Share on other sites
Interesting... I used it no sweat with a socket class. Went fairly smoothly too.

Also, I'm not sure I understand the relevance of your sample.

[edited by - daerid on December 24, 2002 2:43:06 PM]

Share this post


Link to post
Share on other sites
You could've given us a link to the original article. Maybe you misunderstood the author's intent. We'll never know

[edited by - civguy on December 24, 2002 2:49:44 PM]

Share this post


Link to post
Share on other sites
dae: I'm simply asking whats the point in some applications where the class is probably going to stay pretty static.

civ: http://www.cuj.com/experts/1812/hyslop.htm



[edited by - antareus on December 24, 2002 2:58:36 PM]

Share this post


Link to post
Share on other sites
As with any pattern, it''s a solution to a problem you may never have. But in interfaces, it''s very important since they are often designed purely for extending and implementing in different ways. I think the argument is, "if you Might Ever need the Template Method in this class, it should be implemented that way from the start", because it''s impossible to change it over later without breaking the interface.. If you can guarantee your original design is adequate, then it''s not a problem.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
How can it possibly be 2 functions instead of one? Instead of determining the function at runtime via a function pointer, the location of the function is determined at compile time, removing the dereference of the virtual function pointer.

About the only time I can think of skipping it is when executable size is an issue, and speed isn''t.

Whoooosh....

Share this post


Link to post
Share on other sites