Template Method Pattern
Template Method Pattern
In this post I’ll reveal another useful pattern in the world of
design patterns – the template method pattern.
You can read my previous posts about design patterns here:
Structural patterns
Decorator pattern
Proxy pattern
Facade pattern
Adapter pattern
Composite pattern
Bridge pattern
Flyweight pattern
Creational patterns
Singleton pattern
Abstract Factory pattern
Prototype pattern
Factory Method pattern
Builder pattern
Behavioral Patterns
Strategy pattern
Iterator pattern
Template Method Pattern
The template method pattern is used to defer steps of an algorithm
to a class subclasses. The structure of the algorithm doesn’t change
only the “holes” that the pattern leaves (the template method) can defer
from subclass to subclass.
Use Cases for the Template Method Pattern
You should use the pattern in the following cases:
- You have common behavior in an algorithm and
other parts that can be changed. Using the pattern will
help to avoid code duplication. - To enable “hook” operations at specific points in your class.
By doing so you can control the extensions of your class.
UML Diagram
Example in C#
#region Abstract Class
abstract class Abstract
{
public abstract void Step1();
public abstract void Step2();
/// <summary>
/// The template method which call the
/// algorithm parts.
/// </summary>
public void TemplateMethod()
{
Step1();
Step2();
}
}
#endregion
#region Concrete Classes
class Concrete1 : Abstract
{
#region Methods
public override void Step1()
{
Console.WriteLine("Step one was called");
}
public override void Step2()
{
Console.WriteLine("Step two was called");
}
#endregion
}
class Concrete2 : Abstract
{
#region Members
private int _calculation;
#endregion
#region Ctor
/// <summary>
/// Construct a new Concrete2 object
/// </summary>
public Concrete2()
{
_calculation = 0;
}
#endregion
#region Methods
public override void Step1()
{
_calculation++;
Console.WriteLine("calculation is {0}",
_calculation);
}
public override void Step2()
{
_calculation--;
Console.WriteLine("calculation is {0}",
_calculation);
}
#endregion
}
#endregion
The example is self explanatory. The algorithm
has two steps that every concrete class decide how to
implement them.
“Hook” Operation Example
The following example shows how you can “hook” operation
like creating a data accessor object in a derived class of
BaseDataAccessor:
public abstract class BaseDataAccessor
{
#region Members
protected object _dataAccessor;
#endregion
#region Ctor
public BaseDataAccessor()
{
InitDataAccessor();
}
#endregion
#region Methods
protected abstract void InitDataAccessor();
#endregion
}
public class SqlDALComponent : BaseDataAccessor
{
#region Properties
public System.Data.SqlClient.SqlCommand DataAccessor
{
get
{
return ((System.Data.SqlClient.SqlCommand)_dataAccessor);
}
}
#endregion
#region Methods
protected override void InitDataAccessor()
{
_dataAccessor = new System.Data.SqlClient.SqlCommand();
}
#endregion
}
The InitDataAccessor is the “hook” operation that is implemented
in the subclasses and creates the data accessor.
Pay attention that it is only an example and not the way I would
implement a data accessor creation!
Summary
To sum up the post, I introduced the template method pattern.
The template method pattern helps me a lot as a infrastructure
developer to enable “hook” operations to control the extensions
of the classes I build. The template method pattern resemble the
strategy pattern. The only difference between them is that template
method use inheritance to vary part of an algorithm and strategy use
delegation instead.The next post in this series will describe the command
pattern in details.