Click here to Skip to main content
Click here to Skip to main content
Go to top

Template Method Design Pattern in .Net

, 9 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Template method design pattern falls under the category of Behavioral Design Pattern. In this pattern, a template method defines a skeleton of an algorithm in terms of abstract operations. The template method can contain one or more steps. But these steps will have to be in abstract form only. That

Template method design pattern falls under the category of Behavioral Design Pattern. In this pattern, a template method defines a skeleton of an algorithm in terms of abstract operations. The template method can contain one or more steps. But these steps will have to be in abstract form only. That said, we cannot change the order of steps, and most importantly we cannot override the template method itself. Only the steps given in the skeleton of algorithm of template method need to be overridden in concrete classes.

Let’s see how classes can be designed in this template pattern.

emplateMethodPattern

Fig: High Level Class Diagram of Template Method Design Pattern

And see the code implementation below: 

namespace BehavioralDesignPattern.TemplateMethod
{
public abstract class AbstractAlgorithmSkeleton
{
public void TemplateMethod()
{
// Template Method declaring algorithm
// in terms of abstract operations.
Step1();
Step2();
Step3();
}

public abstract void Step1();
public abstract void Step2();
public abstract void Step3();
}

# region “Concrete Implementations of abstract operations defined in Template Method”

public class ConcreteClassA : AbstractAlgorithmSkeleton
{
public override void Step1()
{
Console.WriteLine(“ConcreteClassA, Step 1″);
}

public override void Step2()
{
Console.WriteLine(“ConcreteClassA, Step 2″);
}

public override void Step3()
{
Console.WriteLine(“ConcreteClassA, Step 3″);
}

public void OtherMethodA()
{
//

}
}

public class ConcreteClassB : AbstractAlgorithmSkeleton
{
public override void Step1()
{
Console.WriteLine(“ConcreteClassB, Step 1″);
}

public override void Step2()
{
Console.WriteLine(“ConcreteClassB, Step 2″);
}

public override void Step3()
{
Console.WriteLine(“ConcreteClassB, Step 3″);
}

public void OtherMethodB()
{
//

}
}

# endregion “Concrete Classes Implementation”
}

We see the concrete classes are overriding the abstract operations defined by the template method in its algorithm. This way template method pattern provides an abstract view of algorithm.

So in practical scenario, this pattern fits only when different types of object instances are required to invoke methods or operations that differ sharply in implementation but the algorithm remaining same. Also, sometimes when are refactoring multiple classes, we can find template method pattern coming into picture.

Consuming the Template Method

private void CallTemplateMethod()
{
AbstractAlgorithmSkeleton objTemplate = null;
objTemplate = new ConcreteClassA();
// Now this call to TemplateMethod() will direct calls
// to methods in ConcreteClassA.
objTemplate.TemplateMethod();
}

Important point to note here is: the way we are calling TemplateMethod() of ConcreteClassA from base class AbstractAlgorithmSkeleton reminds us of “The Hollywood Principle”- “Do not call us, we will call for you”. That is, child class method is being called from base class. This way of method call is also known as Inversion of Control.

That’s it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dinesh K Mandal
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
Suggestionneed more example Pinmemberrexpan11-Jul-11 17:06 
The theory is very good.
But I think if there are more examples would be better.
Not the abstract example like above but the real-life example of solving a problem.
 
ps: sory for my english

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 10 Jul 2011
Article Copyright 2010 by Dinesh K Mandal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid