Patterns – The Template Pattern

So this is the first in a series of posts that explain the patterns I use in my day-to-day work. Probably the most common is the template pattern. The purpose of this pattern is to control the way in which the inheritors of your class change its behaviour.

Take for example the following class:

	public class Foo
	{
		public virtual void DoStuff()
		{
			ThisMethodMustBeCalledFirst();
			// Stuff gets done here
		}
		
		private void ThisMethodMustBeCalledFirst()
		{
			// Important initalisation stuff here.
		}
	}

Now imagine that someone comes along and inherits from your lovingly crafted class:

	public class Bar
	{
		public override void DoStuff()
		{
			//New stuff gets done here
		}
	}

It is entirely possible that the user of the Bar class will find that the DoStuff method falls over, because ThisMethodMustBeCalledFirst has not been called. Now this could be solved by putting something like this in your documentation:

It is very important that ThisMethodMustBeCalledFirst is called when overriding DoStuff.

A better way is to make DoStuff() a template method:

	public class Foo
	{
		public void DoStuff()
		{
			ThisMethodMustBeCalledFirst();
			OnDoStuff()
		}
		
		protected virtual void OnDoStuff()
		{
			// Stuff gets done here
		}
		
		private void ThisMethodMustBeCalledFirst()
		{
			// Important initialisation stuff here.
		}
	}

By making DoStuff non-virtual and making it now call the virtual OnDoStuff it is now impossible for an inheritor to change the behaviour of DoStuff without ThisMethodMustBeCalledFirst being called!

So how does this apply to my day-job? Well, the framework I’m working on for the future development of tradesimple will have a base class that will contain all the functionality that is common across all processors. Developers of new processors will override an “OnProcess” method which will be called by the base class as part of a template “Process” method. Not only will this allow us to ensure that certain steps are always followed inside that Process method, but that will also enable us to debug problems more easily by dropping in a debug version of the base class. The Process method inside this base class can do whatever it likes (logging inputs for example) without fear that a badly behaved derived class will override it.

More Resources

  • Obviously there’s the GOF book. Most, if not all the patterns I describe will be in this must-read book
  • Also, the wikipedia article I linked to above has some nice examples.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: