Template Method Design Pattern Using C#

The Template method is one of the behavioral patterns described in the book "Elements Of Reusable Object-Oriented Software" by Gamma et. al. It is also known as a pattern that helps encapsulating algorithms so that subclasses can hook themselves right into a computation anytime they want. It is also known as a design principle inspired by Hollywood: "Don't call us, we will call you".
What it is all about

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.

Problem Description

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.


Solution Description

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.

Source Code
The following shows the Abstract Template Method:
  1. public abstract class IAlgorithm   
  2. {  
  3.     public abstract void Exceute();  
  4. }  
The following shows an abstract class with a few primitives and a Template Method:
  1. public abstract class ICommand: IAlgorithm {  
  2.     /// <summary>  
  3.     /// The execute method contains the order of a  
  4.     /// command that should be executed, deferring some of the  
  5.     /// algorithmic steps to subclasses  
  6.     /// </summary>  
  7.     public sealed override void Exceute() {  
  8.         IsPowerOn();  
  9.         CheckTheStatusOfDevice();  
  10.         SwitchOnOffDevice();  
  11.     }  
  12.     /// <summary>  
  13.     /// Shared Function  
  14.     /// </summary>  
  15.     /// <returns></returns>  
  16.     private bool IsPowerOn() {  
  17.         Console.WriteLine("Yes, Power is on");  
  18.         return true;  
  19.     }  
  20.     /// <summary>  
  21.     /// All base classes should have  
  22.     /// their own implementation to check if device is on or off.  
  23.     /// </summary>  
  24.     protected abstract void CheckTheStatusOfDevice();  
  25.     /// <summary>  
  26.     /// All bases classes should know how to  
  27.     /// swithc on or off  
  28.     /// </summary>  
  29.     protected abstract void SwitchOnOffDevice();  
  30. }  
  31. }  
Concrete Class
  1. public class SwitchOnWaschmachine: ICommand {  
  2.     private readonly Waschmachine _waschmachine;  
  3.     private bool isDeviceOn {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public SwitchOnWaschmachine(Waschmachine waschmachine) {  
  8.         _waschmachine = waschmachine;  
  9.     }  
  10.     protected override void SwitchOnOffDevice() {  
  11.         if (isDeviceOn) {  
  12.             Console.WriteLine("Device is Already On");  
  13.         } else {  
  14.             _waschmachine.SwitchOn();  
Washmachine
  1. public bool _deviceStatus {  
  2.     get;  
  3.     private set;  
  4. }  
  5. internal void SwitchOn() {  
  6.     MessageBox.Show("Switching On Washing Machine");  
  7.     _deviceStatus = true;  
  8. }  
  9. internal void SwitchOff() {  
  10.     MessageBox.Show("Switching Off Washing Machine");  
  11.     _deviceStatus = false;  
  12. }  
Subclass SwitchOnWaschmachine of ICommand implements all the primitive operations that are part of an algorithm. The algorithm that is implemented in a sealed method "Execute" in the base class. When the Execute method is called, it internally calls all the instance methods and primitives (abstract) methods from subclasses in the order defined. Because it's the method that contains the algorithm, this pattern is called a Template Method.
 
Conclusion

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".