Template Method Pattern in C# – Real-Time Example (Algorithm Skeleton)
What is Template Method Pattern?
The Template Method Pattern is a behavioral design pattern that defines the overall structure (template) of an algorithm in a base class, while allowing subclasses to implement specific steps.
👉 Common workflow stays fixed
👉 Individual steps can vary
Why Use Template Method Pattern?
- Reuse common workflow logic
- Avoid duplicate code
- Standardize process flow
- Allow customizable steps
- Improve maintainability
Real-Time Scenario
Report Generation:
- Fetch data
- Format report
- Export report
Different report types:
- PDF Report
- Excel Report
👉 Workflow is same, implementation differs.
Real-Time Example – Report Generation System
Step 1: Abstract Base Class
public abstract class ReportGenerator
{
// Template Method
public void GenerateReport()
{
GetData();
FormatReport();
ExportReport();
}
protected abstract void GetData();
protected abstract void FormatReport();
protected abstract void ExportReport();
}
Step 2: Concrete Implementation – PDF Report
public class PdfReportGenerator : ReportGenerator
{
protected override void GetData()
{
Console.WriteLine("Fetching PDF Data");
}
protected override void FormatReport()
{
Console.WriteLine("Formatting PDF Report");
}
protected override void ExportReport()
{
Console.WriteLine("Exporting PDF Report");
}
}
Step 3: Concrete Implementation – Excel Report
public class ExcelReportGenerator : ReportGenerator
{
protected override void GetData()
{
Console.WriteLine("Fetching Excel Data");
}
protected override void FormatReport()
{
Console.WriteLine("Formatting Excel Report");
}
protected override void ExportReport()
{
Console.WriteLine("Exporting Excel Report");
}
}
Usage Example
ReportGenerator report =
new PdfReportGenerator();
report.GenerateReport();
Output
Fetching PDF Data
Formatting PDF Report
Exporting PDF Report
Key Concept
Instead of:
CreatePdfReport();
CreateExcelReport();
With duplicated workflow ❌
We define:
GenerateReport()
👉 Common structure reused.
Diagram Understanding
ReportGenerator
↓
-------------------------
PdfReportGenerator
ExcelReportGenerator
👉 Base class controls algorithm flow.
Advantages
- ✔ Reuse common logic
- ✔ Reduces duplicate code
- ✔ Standardized workflow
- ✔ Easy to extend
- ✔ Better maintainability
Disadvantages
- ✖ Inheritance-based design
- ✖ Less flexible than composition
- ✖ Changes in base class affect subclasses
When to Use
Use Template Method when:
- Multiple processes share same workflow
- Only some steps vary
- Standardized process flow is needed
- Duplicate algorithm structure exists
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Report generation | Template Method |
| File export systems | Template Method |
| ETL/Data processing | Template Method |
| Payment workflow | Template Method |
| Email templates | Template Method |
ASP.NET Core Real Example
Controller Lifecycle
Request
↓
Authentication
↓
Authorization
↓
Action Execution
↓
Response
👉 Framework defines template flow.
Advanced Example – Payment Processing
ProcessPayment()
↓
Validate
↓
Calculate Charges
↓
Complete Transaction
👉 Different payment providers override specific steps.
Template Method vs Strategy
| Template Method | Strategy |
|---|---|
| Uses inheritance | Uses composition |
| Workflow fixed in base class | Behavior interchangeable |
| Algorithm structure controlled | Algorithms fully replaceable |
Pro Tip
Template Method works well with:
- Base service classes
- Report engines
- Workflow systems
- ETL pipelines
- Framework development
Summary
Template Method helps you:
- Define reusable workflows
- Avoid duplicate logic
- Standardize algorithms
👉 Perfect for:
- Report systems
- Export pipelines
- Workflow engines
- Reusable business processes
Comments
Post a Comment