The Template Method Design Pattern using C# .Net

Posted by nijhawan.saurabh on ASP.net Weblogs See other posts from ASP.net Weblogs or by nijhawan.saurabh
Published on Mon, 15 Oct 2012 20:45:00 GMT Indexed on 2012/10/16 5:05 UTC
Read the original article Hit count: 377

First of all I'll just put this pattern in context and describe its intent as in the GOF book:

 

Template Method:

Define the skeleton of an algorithm in an operation, deferring some steps to

Subclasses. Template Method lets subclasses redefine certain steps of an algorithm

without changing the Algorithm's Structure. 

 

Usage:

When you are certain about the High Level steps involved in an Algorithm/Work flow you can use the Template Pattern which allows the Base Class to define the Sequence of the Steps but permits the Sub classes to alter the implementation of any/all steps.

 

Example in the .Net framework:

The most common example is the Asp.Net Page Life Cycle. The Page Life Cycle has a few methods which are called in a sequence but we have the liberty to modify the functionality of any of the methods by overriding them.

 

Sample implementation of Template Method Pattern:

 

Let's see the class diagram first:           

 

And here goes the code:

EmailBase.cs

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 using System.Threading.Tasks;

    6 

    7 namespace TemplateMethod

    8 {

    9     public abstract class EmailBase

   10     {

   11 

   12         public bool SendEmail()

   13         {

   14             if (CheckEmailAddress() == true) // Method1 in the sequence

   15             {

   16                 if (ValidateMessage() == true) // Method2 in the sequence

   17                 {

   18                     if (SendMail() == true) // Method3 in the sequence

   19                     {

   20                         return true;

   21                     }

   22                     else

   23                     {

   24                         return false;

   25                     }

   26 

   27                 }

   28                 else

   29                 {

   30                     return false;

   31                 }

   32 

   33             }

   34             else

   35             {

   36                 return false;

   37 

   38             }

   39 

   40 

   41         }

   42 

   43         protected abstract bool CheckEmailAddress();

   44         protected abstract bool ValidateMessage();

   45         protected abstract bool SendMail();

   46 

   47 

   48     }

   49 }

   50 

 

 

 EmailYahoo.cs

 

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 using System.Threading.Tasks;

    6 

    7 namespace TemplateMethod

    8 {

    9     public class EmailYahoo:EmailBase

   10     {

   11 

   12         protected override bool CheckEmailAddress()

   13         {

   14             Console.WriteLine("Checking Email Address : YahooEmail");

   15             return true;

   16         }

   17         protected override bool ValidateMessage()

   18         {

   19             Console.WriteLine("Validating Email Message : YahooEmail");

   20             return true;

   21         }

   22 

   23 

   24         protected override bool SendMail()

   25         {

   26             Console.WriteLine("Semding Email : YahooEmail");

   27             return true;

   28         }

   29 

   30 

   31     }

   32 }

   33 

 

 EmailGoogle.cs

 

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 using System.Threading.Tasks;

    6 

    7 namespace TemplateMethod

    8 {

    9     public class EmailGoogle:EmailBase

   10     {

   11 

   12         protected override bool CheckEmailAddress()

   13         {

   14             Console.WriteLine("Checking Email Address : GoogleEmail");

   15             return true;

   16         }

   17         protected override bool ValidateMessage()

   18         {

   19             Console.WriteLine("Validating Email Message : GoogleEmail");

   20             return true;

   21         }

   22 

   23 

   24         protected override bool SendMail()

   25         {

   26             Console.WriteLine("Semding Email : GoogleEmail");

   27             return true;

   28         }

   29 

   30 

   31     }

   32 }

   33 

 

 Program.cs

 

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 using System.Threading.Tasks;

    6 

    7 namespace TemplateMethod

    8 {

    9     class Program

   10     {

   11         static void Main(string[] args)

   12         {

   13             Console.WriteLine("Please choose an Email Account to send an Email:");

   14             Console.WriteLine("Choose 1 for Google");

   15             Console.WriteLine("Choose 2 for Yahoo");

   16             string choice = Console.ReadLine();

   17 

   18             if (choice == "1")

   19             {

   20                 EmailBase email = new EmailGoogle(); // Rather than newing it up here, you may use a factory to do so.

   21                 email.SendEmail();

   22 

   23             }

   24             if (choice == "2")

   25             {

   26                 EmailBase email = new EmailYahoo(); // Rather than newing it up here, you may use a factory to do so.

   27                 email.SendEmail();

   28             }

   29         }

   30     }

   31 }

   32 

 

Final Words:

It's very obvious that why the Template Method Pattern is a popular pattern, everything at last revolves around Algorithms and if you are clear with the steps involved it makes real sense to delegate the duty of implementing the step's functionality to the sub classes.


© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about Behavioral Pattern