Template pattern is a behavioral pattern. Template pattern defines a main process template and this main process template has sub processes and the sequence in which the sub processes can be called. Later the sub processes of the main process can be altered to generate a different behavior.
Punch :- Template pattern is used in scenarios where we want to create extendable behaviors in generalization and specialization relationship.
For example below is a simple process to format data and load the same in to oracle. The data can come from various sources like files, SQL server etc. Irrespective from where the data comes, the overall general process is to load the data from the source, parse the data and then dump the same in to oracle.
Figure: - General Process
Now we can alter the general process to create a CSV file load process or SQL server load process by overriding ‘Load’ and ‘Parse’ sub process implementation.
Figure: - Template thought Process
You can see from the above figure how we have altered ‘Load’ and ‘Parse’ sub process to generate CSV file and SQL Server load process. The ‘Dump’ function and the sequence of how the sub processes are called are not altered in the child processes.
In order to implement template pattern we need to follow 4 important steps:-
public abstract class GeneralParser { protected abstract void Load(); protected abstract void Parse(); protected virtual void Dump() { Console.WriteLine("Dump data in to oracle"); } public void Process() { Load(); Parse(); Dump(); } }
The ‘SqlServerParser’ inherits from ‘GeneralParser’ and overrides the ‘Load’ and ‘Parse’ with SQL server implementation.
public class SqlServerParser : GeneralParser { protected override void Load() { Console.WriteLine("Connect to SQL Server"); } protected override void Parse() { Console.WriteLine("Loop through the dataset"); } }
The ‘FileParser’ inherits from General parser and overrides the ‘Load’ and ‘Parse’ methods with file specific implementation.
public class FileParser : GeneralParser { protected override void Load() { Console.WriteLine("Load the data from the file"); } protected override void Parse() { Console.WriteLine("Parse the file data"); } }
From the client you can now call both the parsers.
FileParser ObjFileParser = new FileParser();ObjFileParser.Process();Console.WriteLine("-----------------------");SqlServerParser ObjSqlParser = new SqlServerParser();ObjSqlParser.Process();Console.Read();
The outputs of both the parsers are shown below.
Load the data from the fileParse the file dataDump data in to oracle-----------------------Connect to SQL ServerLoop through the datasetDump data in to oracle
View my 21 important .NET Interview questions and answers