.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
ASPEvil
Jean Paul
SP
satyapriyanayak
Ashutosh Jha

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

6 common use of Template Design pattern: - Design pattern series

Posted By:Shivprasad koirala       Posted Date: January 06, 2012    Points: 200    Category: ASP.NET    URL: http://www.questpond.com  

In this article we will explain about 6 common use of Template Design pattern.
 

Introduction and Goal
Introduction to template pattern
Scenario 1:- Flexible extendable generalized specialized user interfaces.
Scenario 2:- ASP.NET page life cycle
Scenario 3:- Code generators
Scenario 4:- XML Parser
Scenario 5:- Validation in business components.
Scenario 6:- Customizable logging utility
Visual diagram of design pattern in real time
Design pattern References

Introduction and Goal

In this article we will try to understand 6 important uses of template design patterns. Template design pattern is one of those patterns which are used unknowingly or knowingly in many places. This article will flesh out 6 places where this pattern is seen evident.

Definition :- Template pattern is used in scenarios where we want to create extendable behaviors in generalization and specialization relationship.

If you are new to design patterns you can start with looking at my factory pattern video. It's a pretty old video, so sorry for the quality I was pretty novice when I started recording.

You can also see the last references section where i have written a complete 4 part series on design patterns.

Introduction to template pattern

Template pattern belongs to behavioral pattern category. Template pattern defines a main process template and this main process template calls sub-processes in a sequential manner. Later the sub processes of the main process can be altered to generate a different behavior.

For example below is a simple process to parse data and load the same in to oracle. The overall general process has 3 fixed steps:-

  • Load data from the source
  • Parse the data
  • Dump the same in to oracle

Now you can alter "Load" and "Parse" implementation to create a CSV file load process. The overall sequence of calling "Load", "Parse" and "Dump" will remain same but we have the full liberty to change the implementation of "Load" , "Parse" and "Dump" thus creating a new 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

Now let's walk through some practical scenarios where "Template" pattern can be implemented.

Scenario 1:- Flexible extendable generalized specialized user interfaces.

Many times we come across UI which have all the things almost same but with some small difference in look and feel. For instance in the below screen the data is all same but the one screen has the back ground color different than other.

In this scenario in the form constructor will become the main process which will call three processes/functions: -

  • InitializeComponent :- This will create the UI objects need for the form.
  • LoadCustomer :- This function will load data and bind to the grid.
  • LoadGrid :- This function will define the look and feel of the grid.
public Form1()
        {
            InitializeComponent();
            LoadCustomer();
            LoadGrid();
        }

Below is the full code of the base form. Please note the "LoadGrid" is an abstract method. In other words we can create forms with different color implementation without disturbing the rest of the part of the form.

public abstract partial class Form1 : Form
{
protected Customers objCustomers = new Customers();
       protected List oCustomerList;
       public Form1()
        {
            InitializeComponent();
            LoadCustomer();
            LoadGrid();
        }

        public void LoadCustomer()
        {
            oCustomerList = objCustomers.GetCustomers();
        }
        public abstract void LoadGrid();
        
    }

In other words if you want to create a new form with different color back ground you can keep the other code as it is and just override the "LoadGrid" functionality with a different color / look and feel , below is the sample code for the same.

public partial class Form3 : Form1
{
	....
	....
	....
	....
        public override void LoadGrid()
        {
            dgGridCustomer.DataSource = oCustomerList;
            dgGridCustomer.BackgroundColor = Color.Aqua;
            dgGridCustomer.ForeColor = Color.Brown;
        }
}

Scenario 2: - ASP.NET page life cycle

One very but obvious scenario where we see template pattern very much visible is in ASP.NET page life cycle. In ASP.NET page life cycle the page life cycle sequence is fixed but it provides full authority to override implementation for the each of those sequences.

For instance in ASP.NET page life cycle we have various events with sequence like Init, Load , Validate , Prerender , render etc. Now the sequence is fixed we cannot change them, but yes we can override the implementation for each of these events as per our need.

Scenario 3: - Code generators

Lots of times we generate code by using code generators like the T4 template, LINQ, EF etc from database table design. Now the code generators work on a separate physical file where it generates code for you. So if you change the table design it will regenerate the complete file again.

Now if you want to add some custom code you cannot change the auto generated code file because your code will be replaced when the DB design changes.

So the best approach would be to be extend the code generated class using a separate file and put your custom code in that class. This extension can be done very effectively using template pattern.

The code generated class can define a fixed process but at the same time provide empty virtual methods, properties and functions which can be extended to inject your custom logic.

Scenario 4:- XML Parser

The other scenario which is applicable for template pattern is the XML parser. In XML we normally parse the parent and the child elements. In many scenarios the parsing is almost common with minor child element changes.

For instance in the below code snippet we have Customer as the parent element and every customer will have orders and orders will products.

Now the parsing of Customer and Order elements will be same but the products tag can have size property depending on situation.

For instance in the below code snippet the product element has only name of the product and amount.

<Customer Name="Shiv">
<Orders OrderNumber="1001">
<Product Name="Shirts" Amount="1000"/>
<Product Name="Socks" Amount="100"/>
</Orders>
</Customer>

There can be situations where your product element can have other variations as shown in the below XML snippet. In this case you can just override the parsing process of product element and keep the overall XML parsing process sequence same.

<Customer Name="Shiv">
<Orders OrderNumber="1001">
<Product Name="Shirts" Amount="1000">
<LargeSize/>
</Product>
<Product Name="Socks" Amount="1000">
<SmallSize/>
</Product>
</Orders>
</Customer>

Scenario 5:- Validation in business components.

Business classes have validation and we would like to create different versions of business classes with different validation logic.

public class Supplier
    {
        private string _SupplierCode;

        public string SupplierCode
        {
            get 
            { 
                return _SupplierCode; 
            }
            set 
            {
                ValidateSuppCode(value);
                _SupplierCode = value; 
            }
        }

        public virtual  void ValidateSuppCode(string SuppCode)
        {
            if (SuppCode.Length == 0)
            {
                throw new Exception("Can not be null");
            }
        }
    }
public class SupplierNew : Supplier
    {
        public override void ValidateSuppCode(string SuppCode)
        {
            base.ValidateSuppCode(SuppCode);
            if (SuppCode.Length > 10)
            {
                throw new Exception("can not be more than 10");
            }
        }
    }

Scenario 6:- Customizable logging utility

This is one more scenario where template pattern fits like anything. If you look at these components i.e. message loggers, error loggers etc they execute in two phases, in the first phase they prepare the message and in the second phase they log it.

So for these kinds of scenarios we can create a parent class who defines two fixed sequences one which does preparation of message and other which logs the message to the source (file, event viewer, email etc).

Later we can create child classes who can inherit and override the logic of those phases but keeping the sequence intact.

Visual diagram of design pattern in real time: -

Design pattern References

Part 1 Design pattern FAQ's -- factory pattern, abstract factory pattern, builder pattern, prototype pattern, singleton pattern and command pattern

Part 2 Design Pattern FAQ's -- Interpreter pattern, iterator pattern, mediator pattern, memento pattern and observer pattern

Part 3 Design Pattern FAQ's -- state pattern, strategy pattern, visitor pattern, adapter pattern and fly weight pattern

Part 4 Design Pattern FAQ's -- Bridge pattern, composite pattern, decorator pattern, Facade pattern, chain of responsibility(COR), proxy pattern and template pattern

Feel free to download these FAQ PDF's, own articles of design patterns from my site and also I have collected around 400 FAQ questions and answers in Silverlight, Azure, VSTS, WCF, WPF, WWF, SharePoint, design patterns, UML etc.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend