Template Method Design Pattern - C#
Hi,
In this article, we will be looking about the template method design pattern.
The basic definition of Template Method Design Pattern says, “It defines the skeleton of an algorithm in a method(s) and allowing subclasses to have their own implementation with the Abstract Methods which are defined in the base class."
We will look into the Template Method Design pattern with an example.
Let's assume, that we are going to fetch some data from the file and we will be updating or inserting into the database. We will design our class so that any class which is going to inherit the base will have some common functionality. As I said before, we are going to implement the Template Method Design Pattern with reading a file. It can be of any type. Sub class will decide which type of file it wants to read.
We are going to handle this with 3 classes.
1. ProcessEmployee ( Abstract Class)
2. ExcelFileEmployee
3. TextFileEmployee
Employee.cs
public class Employee
{
int _employeeNumber;
string _employeeName;
public int EmployeeNumber
{
get { return _employeeNumber; }
set { _employeeNumber = value; }
}
public string EmployeeName
{
get { return _employeeName; }
set { _employeeName = value; }
}
}
ProcessEmployee.cs
public abstract class ProcessEmployee
{
List
public List
{
get { return _allEmployees; }
set { _allEmployees = value; }
}
public abstract void GetEmployeeFromFile();
public void ValidateEmployee(List
{
// check whether data which comes invalid or not
}
public void InsertEmployee(List
{
// insert the employee object into the database.
}
}
ExcelFileEmployee.cs
public class ExcelFileEmployee : ProcessEmployee
{
public override void GetEmployeeFromFile()
{
// read the excel file and assign it to the property AllEmployees;
AllEmployees = GetDataFromExcelFile();
}
private List
{
// implement your code to read the data
// from Excel File and assign it to the Employee Collection List
return new List
}
}
TextFileEmployee.cs
public class TextFileEmployee : ProcessEmployee
{
public override void GetEmployeeFromFile()
{
// read the excel file and assign it to the property AllEmployees;
AllEmployees = GetDataFromTextFile();
}
private List
{
// implement your code to read the data
// from TExt File and assign it to the Employee Collection List
return new List
}
}
I have defined all 4 classes which I will be using in this article. My requirement is, I have to design a class which will have some basic implementation and abstract method and the class which inherits this base class will implement the abstract method the way they want.
I have an abstract class which has GetEmployeeFromFile as an Abstract method and the methods, ValidateEmployee and InsertEmployee implemented. Whenever any class functionality requires, that they want to get the data, validate the data and insert into the database, then that class will be inheriting from the class ProcessEmployee.
That's what you are seeing in both the classes ExcelFileEmployee and TextFileEmployee. I made GetEmployeeFromFile as abstract because, any class which inherits the base class can have their own implementation made in that. But the validation and insertion algorithm of the employee records are going to be same. That's the reason I made ValidateEmployee and InsertEmployee as a concrete methods so that these functionalities can be shared when the abstract class inherited.
Again, coming back to the definition of the Template Method Pattern, “It defines the skeleton of an algorithm in method(s) and allowing subclasses to have their own implementation with the Abstract Methods which are defined in the base class."
Here, our base class is ProcessEmployee. This is having an abstract method(GetEmployeeFromFile) and 2 concrete methods(ValidateEmployee and InsertEmployee). The class which inherits the abstract class ProcessEmplyee can have their own implementation for the method GetEmployeeFrom file and they can make use of the ValidateEmployee and InsertEmployee which are already implemented in the base class. Because the algorithm which we will be using in these 2 methods are going to be same for any type of file where we will be getting the records for the employee and inserting into the database.
Hope it gives some basic idea about Template Method Design Pattern. Just go and review the code what's written in your application and if possible just figure it out where you can use the Template Method Design Pattern.
If you have any queries, please feel to post here. I will be happy to answer those queries.
Regards,
Brainstorming Guy aka Venkatarajan A
Nice article..The example was great