intelliproject logo

Location: Desktop development - C/C++    License: The Intelliproject Open License (IPOL)

The Con-Template Pattern

Posted by Wong Shao Voon

Show how to use the Template Pattern

Skill: Beginner

Posted: 12/09/2010

Views: 773

Rating: 5.00 /5

Popularity: 1.51

Sign Up to vote for this article

Introduction

This is a simple article on Template Pattern. Template Pattern is one of the widely used patterns. It allows us to set up the outline of an algorithm and leaves the details of the implementation later. Template Pattern is a behaviour pattern. Let us have a look at the UML diagram below.

TemplePattern.png

TemplateMethod is a public non-virtual method while Operation is a protected pure-virtual method whose behaviour will be defined in the ConcreteClassA and ConcreteClassB. Even though TemplateMethod is a non-virtual method but due to its call to the Operation method, its behaviour is partially defined by Operation method in the polymorphic derived class.

A Simple Non-Templated Example

Let us look at the problem this pattern can solve. I have a TextOut base class which is meant to be used in the derived classes, say Console, Debug and File to print out the contents in console, debugger and file logging, respectively. The TextOut class has 10 virtual Print methods which I need to override in my derived classes. These Print method use the Box object as their parameters. The Box class is just a simple class whose constructor takes in a primitive data type(POD), and converts them to std::wstring.

These are 2 examples of what the Print methods are doing. I will not show the rest of them as they are similar in nature.

Below is the declaration of the Console class

Below is an example how I would override the Print methods without using Template Pattern.

Remember I have to repeat this boilerplated code 8 more times. Here is an example on how to call the Console::Print method.

A Simple Templated Fix

Using the template pattern, I can just define this above code just once. Below is an abstract Textout class to take advantage of Template Pattern.

All the Print methods in this AbstractTextout are changed to non-virtual. I have removed the GetFormattedString method and added a pure-virtual Process method and the Print methods will call this Process method which I need to implement in the derived class. Below are the 2 new Print method definitions. Notice there is little changes, except it calls the pure-virtual Process method with the formatted string.

Below is the new Console class declaration. We need to implement the Process method!

Below are the definition of Console's constructor, destructor and its only method (which is Process).

You can read the source code of the Debug and File class if you are interested.

Here is an example on how to call these classes' Print method. As you may have noticed, the way to call Console::Print method has not changed!

Conclusion

We have looked an example of how Template Pattern is applied to a problem. This is a rather unique problem which is solved by Template Pattern. For most of time, we use Template Pattern to define the behaviour of our class later. As for the reason why I titled this article, "The Con-Template Pattern", is because the Template Pattern 'con' a non-virtual method to have polymorphic behaviour by calling a polymorphic method. And we should contemplate on this wonderful pattern. Well, I cannot say I titled it this way to 'con' you to read my article, can I? Oops!!

Thank you for reading!

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Wong Shao Voon

I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me.

When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day.

I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts.

I also writes articles for IntelliProject; I have a few ideas to write about but never get around writing because of hectic schedule.

Location: Singapore
Ocupation: Software Developer

Sign up to post message on the article message board!