- Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
- Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
- The intent of the Template Method design pattern is to provide an outline of a series of steps for an algorithm.
- Derived classes retain the original structure of the algorithm but have the option to redefine or adjust certain steps of the algorithm.
- This pattern is designed to offer extensibility to the client programmer.
- Template Methods are frequently used when building a class library (for example an application framework) that is going to be used by other client programmers.
- There are strong similarities between the Template Method and the Strategy pattern.
- Both are designed for extensibility and customization as they allow the client to alter the way an algorithm or process is executed.
- The difference is that with Strategy the entire algorithm is changed, whereas the Template method allows individual steps to be redefined.
- However, their object-oriented implementations are quite different:
- Strategy uses delegation and Template Method is based on object inheritance.
- It is through this pattern that .NET provides extensibility to the users of its API.
- Take a look at custom controls in ASP.NET. Custom controls are created by inheriting from one of the control base classes (Control or WebControl).
- Out of the box these base classes handle all functionality that are common to all controls.
- E.g. initializing, loading, rendering, unloading, and firing control lifecycle events at the right time.
- Your custom control extends the functionality of these base classes by overriding individual steps in the control generation algorithm.
- E.g. the Render and CreateChildControls methods as well as handling certain events, such as the Postback event.
Participants
AbstractClass
(DataAccessObject)
- Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
- Implements a template method (
Run
) defining the skeleton of an algorithm.
- The template method calls primitive operations as well as operations defined in the
AbstractClass
or those of other objects.
ConcreteClass
(Categories, Products)
- Implements the primitive operations to carry out subclass-specific steps of the algorithm.
DataAccessObject daoCategories = new Categories();
daoCategories.Run();
DataAccessObject daoProducts = new Products();
daoProducts.Run();
public abstract class DataAccessObject
{
protected string _ConnectionString;
protected DataSet _DataSet;
public virtual void Connect() => _ConnectionString = "provider=Microsoft.JET.OLEDB.4.0; data source=..\\\\..\\\\..\\\\db1.mdb";
public abstract void Select();
public abstract void Process();
public virtual void Disconnect() => _ConnectionString = string.Empty;
// The 'Template Method'.
public void Run()
{
Connect();
Select();
Process();
Disconnect();
}
}
public class Categories : DataAccessObject
{
public override void Select()
{
var sql = "SELECT CategoryName FROM Categories";
var dataAdapter = new SqlDataAdapter(sql, _ConnectionString);
_DataSet = new DataSet();
dataAdapter.Fill(_DataSet, "Categories");
}
public override void Process()
{
var dataTable = _DataSet.Tables["Categories"];
foreach (DataRow row in dataTable.Rows)
Console.WriteLine(row["CategoryName"]);
}
}
public class Products : DataAccessObject
{
public override void Select()
{
var sql = "select ProductName from Products";
var dataAdapter = new SqlDataAdapter(sql, _ConnectionString);
_DataSet = new DataSet();
dataAdapter.Fill(_DataSet, "Products");
}
public override void Process()
{
var dataTable = _DataSet.Tables["Products"];
foreach (DataRow row in dataTable.Rows)
Console.WriteLine(row["ProductName"]);
}
}
Rules of Thumb
- Strategy is like Template Method except in its granularity.
- Template Method uses inheritance to vary part of an algorithm.
- Strategy uses delegation to vary the entire algorithm.
- Strategy modifies the logic of individual objects.
- Template Method modifies the logic of an entire class.
- Factory Method is a specialization of Template Method.