SOLID Principles with Real-World Examples
This demonstrates the Factory Pattern combined with the Template Method Pattern.
Report (Abstract Base Class)
├── PDF
├── DOCX
├── TEXT
└── SpecialReport (Abstract)
└── PPTX
public abstract class Report
{
protected abstract void Create();
protected abstract void Parse();
protected abstract void Validate();
protected abstract void Save();
public virtual void GenerateReport() // Template Method
{
Create();
Parse();
Validate();
Save();
}
}
Purpose: Defines the common structure and workflow for all reports.
GenerateReport()
defines the algorithm skeletonpublic abstract class SpecialReport : Report
{
protected abstract void ReValidate();
public override void GenerateReport() // Modified workflow
{
Create();
Parse();
Validate();
ReValidate(); // Additional step
Save();
}
}
Purpose: Extends the base workflow with additional validation step.
Each concrete class implements the abstract methods:
public class PDF : Report
{
protected override void Create() { Console.WriteLine("PDF Created"); }
protected override void Parse() { Console.WriteLine("PDF Parsed"); }
protected override void Validate() { Console.WriteLine("PDF Validated"); }
protected override void Save() { Console.WriteLine("PDF Saved"); }
}