In this article, I am going to discuss about Template Method pattern. This pattern helps us to improve code reusability. Without this pattern, it may require to write duplicated code in many subclasses. It is very helpful in designing frameworks. This pattern defines the skeleton of a method/operation and allowing subclasses to define some steps of it. Template methods allow subclasses to redefine few of the steps of a method/algorithm without changing its structure/flow. The main theme behind this pattern is to give a chance to subclasses for redefining few operations/methods. I am going to explain this pattern with an example.
Scenarios applicable for using this Pattern:
-
To reduce replicated code in subclasses.
-
To maintain the code properly for easy modifications.
Benefits of using this Pattern:
-
To improve Code Reusability.
-
Helpful in designing Frameworks.
Participants in this Pattern:
-
Abstract Class: This class defines virtual and template methods. This template method will have calls to methods defined in concrete subclasses.
-
Concrete Class: This class defines the methods inherited from base class along with definition to its internal members.
Implementation:
This example explains about Template Method Display(), which is having a skeleton calling a series of methods. Implementation of some of the methods is given to its subclasses.
Create a new console application and name it as TemplatePatternSample. Now, add the code to program.cs as shown below:
First, I am defining Employee & Department classes [DTO] as shown below:
#region DTO Defns
class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string Salary { get; set; }
public Employee(int empID, string empName, string sal)
{
this.EmpID = empID;
this.EmpName = empName;
this.Salary = sal;
}
}
class Department
{
public int DeptID { get; set; }
public string DeptName { get; set; }
public string Location { get; set; }
public Department(int deptID, string deptName, string loc)
{
this.DeptID = deptID;
this.DeptName = deptName;
this.Location = loc;
}
}
#endregion
Now, we will define the abstract class named as AbstractDAO:
#region Abstract Class
abstract class AbstractDAO
{
public abstract void LoadDetails();
public abstract void FormatAndDisplayDetails();
public virtual void StartUp()
{
Console.WriteLine("Start of Processing");
Console.WriteLine("-------------------");
}
public virtual void Dispose()
{
Console.WriteLine("End of Processing");
}
public void Display()
{
StartUp();
LoadDetails();
FormatAndDisplayDetails();
Dispose();
}
}
#endregion
Here, we defined Display() method with a series of method calls. LoadDetails() and FormatAndDisplayDetails() are declared as abstract making it to redefine in its subclasses.
We will define two concrete subclasses for AbstractDAO as shown below:
#region Concrete Classes
class Employees : AbstractDAO
{
private List<Employee> empList = new List<Employee>();
public override void LoadDetails()
{
//Get Data From Database.
empList.Add(new Employee(10,"TEST","2000"));
empList.Add(new Employee(20,"TEST1","3000"));
empList.Add(new Employee(30, "TEST2", "4000"));
}


Join the conversation! Your thoughts help the community grow.