Tutorials Design Patterns in C#
Template Method Pattern — Complete Guide
Template Method Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.
On this page
Design Patterns in C# · Lesson 22 of 69
Template Method Pattern
GoF Core ✓ → Enterprise → Cloud & Craft
Enterprise · 2 — App patterns · ~6 min · Module 3: Behavioral Design Patterns
What is this?
Template Method defines an algorithm skeleton in a base class; subclasses override steps without changing the sequence.
Why should you care?
ShopNest “import catalog” always validate → transform → save, but CSV vs Excel differ in parse details.
See it live — copy this example
Paste into a C# console or class library project and run dotnet run.
public abstract class CatalogImport
{
public void Run(string path)
{
var rows = Parse(path);
rows = Transform(rows);
Save(rows);
}
protected abstract List<string> Parse(string path);
protected virtual List<string> Transform(List<string> rows) => rows;
protected abstract void Save(List<string> rows);
}
public sealed class CsvCatalogImport : CatalogImport
{
protected override List<string> Parse(string path) => new() { "HD-100", "HD-200" };
protected override void Save(List<string> rows) => Console.WriteLine($"saved {rows.Count}");
}
new CsvCatalogImport().Run("a.csv");
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Run is the template.
- CsvCatalogImport fills Parse/Save.
- Transform has a default.
- Prefer composition if inheritance gets awkward.
Practice next
- Run CSV import.
- Add ExcelCatalogImport.
- Override Transform to uppercase SKUs.
- Add a BeforeSave hook.
- Log each step in Run.
Remember
Fixed algorithm steps. Override hooks. Shared sequence.
ShopNest catalog import
CSV/Excel importers share validate→save flow.
Outcome: New formats override parse only.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!