Criticism of the Template method

Started by
6 comments, last by antareus 21 years, 3 months ago
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Advertisement
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.
daerid@gmail.com
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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]
daerid@gmail.com
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]
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]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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 ]
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....
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement