The Template Method design pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. This pattern lets subclasses redefine certain steps of an algorithm without changing the algorithm‘s structure.
C# code examples of the Template Method design pattern is provided in 3 forms:
A visualization of the classes and objects participating in this pattern.
The classes and objects participating in this pattern include:
DataObject
)
CustomerDataObject
)
This structural code demonstrates the Template Method which provides a skeleton calling sequence of methods. One or more steps can be deferred to subclasses which implement these steps without changing the overall calling sequence.
using System;
namespace Template.Structural
{
/// <summary>
/// Template Design Pattern
/// </summary>
public class Program
{
public static void Main(string[] args)
{
AbstractClass aA = new ConcreteClassA();
aA.TemplateMethod();
AbstractClass aB = new ConcreteClassB();
aB.TemplateMethod();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'AbstractClass' abstract class
/// </summary>
public abstract class AbstractClass
{
public abstract void PrimitiveOperation1();
public abstract void PrimitiveOperation2();
// The "Template method"
public void TemplateMethod()
{
PrimitiveOperation1();
PrimitiveOperation2();
Console.WriteLine("");
}
}
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
public class ConcreteClassA : AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine("ConcreteClassA.PrimitiveOperation1()");
}
public override void PrimitiveOperation2()
{
Console.WriteLine("ConcreteClassA.PrimitiveOperation2()");
}
}
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
public class ConcreteClassB : AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine("ConcreteClassB.PrimitiveOperation1()");
}
public override void PrimitiveOperation2()
{
Console.WriteLine("ConcreteClassB.PrimitiveOperation2()");
}
}
}
This real-world code demonstrates a Template method named Run() which provides a skeleton calling sequence of methods. Implementation of these steps are deferred to the CustomerDataObject subclass which implements the Connect, Select, Process, and Disconnect methods.
using System;
using System.Collections.Generic;
namespace Template.RealWorld
{
/// <summary>
/// Template Design Pattern
/// </summary>
public class Program
{
public static void Main(string[] args)
{
DataAccessor categories = new Categories();
categories.Run(5);
DataAccessor products = new Products();
products.Run(3);
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'AbstractClass' abstract class
/// </summary>
public abstract class DataAccessor
{
public abstract void Connect();
public abstract void Select();
public abstract void Process(int top);
public abstract void Disconnect();
// The 'Template Method'
public void Run(int top)
{
Connect();
Select();
Process(top);
Disconnect();
}
}
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
public class Categories : DataAccessor
{
private List<string> categories;
public override void Connect()
{
categories = new List<string>();
}
public override void Select()
{
categories.Add("Red");
categories.Add("Green");
categories.Add("Blue");
categories.Add("Yellow");
categories.Add("Purple");
categories.Add("White");
categories.Add("Black");
}
public override void Process(int top)
{
Console.WriteLine("Categories ---- ");
for(int i = 0; i < top; i++)
{
Console.WriteLine(categories[i]);
}
Console.WriteLine();
}
public override void Disconnect()
{
categories.Clear();
}
}
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
public class Products : DataAccessor
{
private List<string> products;
public override void Connect()
{
products = new List<string>();
}
public override void Select()
{
products.Add("Car");
products.Add("Bike");
products.Add("Boat");
products.Add("Truck");
products.Add("Moped");
products.Add("Rollerskate");
products.Add("Stroller");
}
public override void Process(int top)
{
Console.WriteLine("Products ---- ");
for (int i = 0; i < top; i++)
{
Console.WriteLine(products[i]);
}
Console.WriteLine();
}
public override void Disconnect()
{
products.Clear();
}
}
}
The .NET optimized code demonstrates the same code as above but uses
more modern C# and .NET features.
Here is an elegant C# Template solution.
namespace Template.NetOptimized;
using static System.Console;
/// <summary>
/// Template Design Pattern
/// </summary>
public class Program
{
public static void Main()
{
var categories = new CategoryAccessor();
categories.Run(5);
var products = new ProductAccessor();
products.Run(3);
// Wait for user
ReadKey();
}
}
public record Category
{
public string CategoryName { get; set; } = null!;
}
public record Product
{
public string ProductName { get; set; } = null!;
}
/// <summary>
/// The 'AbstractClass' abstract class
/// </summary>
public abstract class DataAccessor<T> where T : class, new()
{
protected List<T> Items { get; set; } = [];
public virtual void Connect()
{
Items.Clear();
}
public abstract void Select();
public abstract void Process(int top);
public virtual void Disconnect()
{
Items.Clear();
}
// The 'Template Method'
public void Run(int top)
{
Connect();
Select();
Process(top);
Disconnect();
}
}
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
public class CategoryAccessor : DataAccessor<Category>
{
public override void Select()
{
Items.Add(new() { CategoryName = "Red" });
Items.Add(new() { CategoryName = "Green" });
Items.Add(new() { CategoryName = "Blue" });
Items.Add(new() { CategoryName = "Yellow" });
Items.Add(new() { CategoryName = "Purple" }); ;
Items.Add(new() { CategoryName = "White" });
Items.Add(new() { CategoryName = "Black" });
}
public override void Process(int top)
{
WriteLine("Categories ---- ");
for (int i = 0; i < top; i++)
{
WriteLine(Items[i].CategoryName);
}
WriteLine();
}
}
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
public class ProductAccessor : DataAccessor<Product>
{
public override void Select()
{
Items.Add(new Product { ProductName = "Car" });
Items.Add(new Product { ProductName = "Bike" });
Items.Add(new Product { ProductName = "Boat" });
Items.Add(new Product { ProductName = "Truck" });
Items.Add(new Product { ProductName = "Moped" });
Items.Add(new Product { ProductName = "Rollerskate" });
Items.Add(new Product { ProductName = "Stroller" });
}
public override void Process(int top)
{
WriteLine("Products ---- ");
for (int i = 0; i < top; i++)
{
WriteLine(Items[i].ProductName);
}
WriteLine();
}
}