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, 1:10 PM EDT
nice article
Yashwanth MuthineniPosted Aug 19, 2015, 2:14 AM EDT
Nice Share
deepti yadavPosted Aug 16, 2015, 10:56 PM EDT
Nice !! :) Keep going :)
Vaikesh K PPosted Aug 16, 2015, 8:40 PM EDT
Nice work sir :)
Santhakumar MunuswamyPosted Aug 15, 2015, 10:21 PM EDT
Nice Article. Thanks for sharing
Sam HobbsPosted Aug 14, 2015, 5:47 PM EDT
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, 10:38 AM EDT
nice start..
Pankaj Kumar ChoudharyTOP 100Posted Aug 14, 2015, 8:51 AM EDT
Nice Start Shakti sir.........
RakeshPosted Aug 14, 2015, 6:50 AM EDT
Good start
Gopi ChandPosted Aug 14, 2015, 3:42 AM EDT
Good work...nice start
Shakti SaxenaPosted Aug 14, 2015, 3:35 AM EDT
Thank you all for the encouragement.
Mohammed IbrahimPosted Aug 14, 2015, 3:22 AM EDT
nice
Karthikeyan KPosted Aug 14, 2015, 3:16 AM EDT
Good start..thanks for sharing
Sibeesh VenuTOP 100Posted Aug 14, 2015, 2:54 AM EDT
Nice Share :)
Rajeesh MenothPosted Aug 14, 2015, 2:22 AM EDT
Nice share,welcome to c-sharp community...