Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure. (Gamma et.al.)
Let's understand the entire usage of the pattern step-by-step with an example.
We have a remote controller that helps us controlling several households at home, for example TV, refridgerator, music and so on. As in the following figure, we only have 2 buttons for each and every slot, ON and OFF on the remote. On clicking a button, a number of steps are followed in a strict order, in other words steps of an algorithm. In addition, some common checks are also done such as if electricity supply is on or off. These checks are done for each and every household device.

We can define an abstract class that contains a method (template method) that in turn contains a few primitive operations that each subclass must implement. The template method must not be overridden by subclasses. That means the algorithm must not be changed by subclasses or say it should be hidden. Having said that, let's introduce the class diagram for the given problem.

The point to be noted here is that though a complete solution allows low-level component (concrete classes) to hook themselves into the system, its the high-level component that determine when a low-level implementation is required. It is also known as the "Hollywood Principle", in other words Don't call us, we will call you.
- public abstract class IAlgorithm
- {
- public abstract void Exceute();
- }
- public abstract class ICommand: IAlgorithm {
- /// <summary>
- /// The execute method contains the order of a
- /// command that should be executed, deferring some of the
- /// algorithmic steps to subclasses
- /// </summary>
- public sealed override void Exceute() {
- IsPowerOn();
- CheckTheStatusOfDevice();
- SwitchOnOffDevice();
- }
- /// <summary>
- /// Shared Function
- /// </summary>
- /// <returns></returns>
- private bool IsPowerOn() {
- Console.WriteLine("Yes, Power is on");
- return true;
- }
- /// <summary>
- /// All base classes should have
- /// their own implementation to check if device is on or off.
- /// </summary>
- protected abstract void CheckTheStatusOfDevice();
- /// <summary>
- /// All bases classes should know how to
- /// swithc on or off
- /// </summary>
- protected abstract void SwitchOnOffDevice();
- }
- }
- public class SwitchOnWaschmachine: ICommand {
- private readonly Waschmachine _waschmachine;
- private bool isDeviceOn {
- get;
- set;
- }
- public SwitchOnWaschmachine(Waschmachine waschmachine) {
- _waschmachine = waschmachine;
- }
- protected override void SwitchOnOffDevice() {
- if (isDeviceOn) {
- Console.WriteLine("Device is Already On");
- } else {
- _waschmachine.SwitchOn();
- public bool _deviceStatus {
- get;
- private set;
- }
- internal void SwitchOn() {
- MessageBox.Show("Switching On Washing Machine");
- _deviceStatus = true;
- }
- internal void SwitchOff() {
- MessageBox.Show("Switching Off Washing Machine");
- _deviceStatus = false;
- }
Since it can be clearly seen from the example that the Template Method enables subclasses hook into the higher level components but its the higher-level component that decides when a lower-level component must be called. That means "Don't call us, we'll calll you".

Vipul MalhotraPosted Sep 6, 2015, 5:10 PM
nice article
Yashwanth MuthineniPosted Aug 19, 2015, 6:14 AM
Nice Share
deepti yadavPosted Aug 17, 2015, 2:56 AM
Nice !! :) Keep going :)
Vaikesh K PPosted Aug 17, 2015, 12:40 AM
Nice work sir :)
Santhakumar MunuswamyPosted Aug 16, 2015, 2:21 AM
Nice Article. Thanks for sharing
Sam HobbsPosted Aug 14, 2015, 9:47 PM
Actually the people in Hollywood are much like people everywhere. My guess is that if you were to go to both Hollywood and New York you would be more likely to hear people in New York say "Don't call us, we'll call you". Perhaps people say "Hollywood" to refer to movies and movies are made about all kinds of people everywhere in the world.
Shridhar SharmaPosted Aug 14, 2015, 2:38 PM
nice start..
Pankaj Kumar ChoudharyPosted Aug 14, 2015, 12:51 PM
Nice Start Shakti sir.........
RakeshPosted Aug 14, 2015, 10:50 AM
Good start
Gopi ChandPosted Aug 14, 2015, 7:42 AM
Good work...nice start
Shakti SaxenaPosted Aug 14, 2015, 7:35 AM
Thank you all for the encouragement.
Mohammed IbrahimPosted Aug 14, 2015, 7:22 AM
nice
Karthikeyan KPosted Aug 14, 2015, 7:16 AM
Good start..thanks for sharing
Sibeesh VenuPosted Aug 14, 2015, 6:54 AM
Nice Share :)
Rajeesh MenothPosted Aug 14, 2015, 6:22 AM
Nice share,welcome to c-sharp community...