Monday, March 21, 2011

.Net Interview Questions :- Can you explain template pattern?

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:-

  1. Create the template or the main process by creating a parent abstract class.
  2. Create the sub processes by defining abstract methods and functions.
  3. Create one method which defines the sequence of how the sub process methods will be called. This method should be defined as a normal method so that we child methods cannot override the same.
  4. Finally create the child classes who can go and alter the abstract methods or sub process to define new implementation.
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 file
Parse the file data
Dump data in to oracle
-----------------------
Connect to SQL Server
Loop through the dataset
Dump data in to oracle


View my 21 important .NET Interview questions and answers

No comments: