Definition
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.
UML class diagram
Participants
The classes and/or objects participating in this pattern are:
- AbstractClass
implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
- ConcreteClass - implements the primitive operations ot carry out subclass-specific steps of the algorithm
Sample code in C#
///
/// MainApp startup class for Real-World
/// Template Design Pattern.
///
class MainApp
{
///
/// Entry point into console application.
///
static void Main()
{
DataAccessObject daoCategories = new Categories();
daoCategories.Run();
DataAccessObject daoProducts = new Products();
daoProducts.Run();
// Wait for user
Console.ReadKey();
}
}
///
/// The 'AbstractClass' abstract class
///
abstract class DataAccessObject
{
protected string connectionString;
protected DataSet dataSet;
public virtual void Connect()
{
// Make sure mdb is available to app
connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source=..\\..\\..\\db1.mdb";
}
public abstract void Select();
public abstract void Process();
public virtual void Disconnect()
{
connectionString = "";
}
// The 'Template Method'
public void Run()
{
Connect();
Select();
Process();
Disconnect();
}
}
///
/// A 'ConcreteClass' class
///
class Categories : DataAccessObject
{
public override void Select()
{
string sql = "select CategoryName from Categories";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( sql, connectionString);
dataSet = new DataSet();
dataAdapter.Fill(dataSet, "Categories");
}
public override void Process()
{
Console.WriteLine("Categories -- ");
DataTable dataTable = dataSet.Tables["Categories"];
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["CategoryName"]);
}
Console.WriteLine();
}
}
///
/// A 'ConcreteClass' class
///
class Products : DataAccessObject
{
public override void Select()
{
string sql = "select ProductName from Products";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( sql, connectionString);
dataSet = new DataSet();
dataAdapter.Fill(dataSet, "Products");
}
public override void Process()
{
Console.WriteLine("Products -- ");
DataTable dataTable = dataSet.Tables["Products"];
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["ProductName"]);
}
Console.WriteLine();
}
}
-
-
Liest(1901)
-
Permalink