Template Method
The template method pattern
The template method is a behavioral pattern [GOF], which is a pattern classification concerned with the way classes or objects interact.This is quite a simple pattern and most programmers would have done this naturally, but there are a couple of subtle twists that can help, expecially with hard to read convoluted code.
The reason for the pattern is to force an order for an algorithm but leave some of the implemention to the higher level class.
Here's a very simple example:
abstract class Instructions {
private void DoThisFirst() {
Console.WriteLine("This has to be done first.");
}
protected abstract void DoThisSecond();
private void SetUpAndDoThird() {
Console.WriteLine("Setting up for third process...");
DoThisThirdHook();
}
protected abstract void DoThisThirdHook();
protected virtual void DoThisFourthDefault() {
Console.WriteLine("This is the fourth(default).");
}
public void ProcessIntructions() {
Console.WriteLine("ProcessInstructions:");
DoThisFirst();
DoThisSecond();
SetUpAndDoThird();
DoThisFourthDefault();
}
}
and the implementation:
private void DoThisFirst() {
Console.WriteLine("This has to be done first.");
}
protected abstract void DoThisSecond();
private void SetUpAndDoThird() {
Console.WriteLine("Setting up for third process...");
DoThisThirdHook();
}
protected abstract void DoThisThirdHook();
protected virtual void DoThisFourthDefault() {
Console.WriteLine("This is the fourth(default).");
}
public void ProcessIntructions() {
Console.WriteLine("ProcessInstructions:");
DoThisFirst();
DoThisSecond();
SetUpAndDoThird();
DoThisFourthDefault();
}
}
class Markings : Instructions {
protected override void DoThisSecond() {
Console.WriteLine("Markings: Second mark");
}
protected override void DoThisThirdHook() {
Console.WriteLine("Markings: Third mark");
}
protected override void DoThisFourthDefault() {
// can choose to do the default or override
base.DoThisFourthDefault();
}
}
the code to test:
protected override void DoThisSecond() {
Console.WriteLine("Markings: Second mark");
}
protected override void DoThisThirdHook() {
Console.WriteLine("Markings: Third mark");
}
protected override void DoThisFourthDefault() {
// can choose to do the default or override
base.DoThisFourthDefault();
}
}
static void Main(string[] args) {
Instructions markings = new Markings();
markings.ProcessIntructions();
Console.ReadKey();
}
and finally the output:
Instructions markings = new Markings();
markings.ProcessIntructions();
Console.ReadKey();
}
ProcessInstructions:
This has to be done first.
Markings: Second mark
Setting up for third process...
Markings: Third mark
This is the fourth(default).
This has to be done first.
Markings: Second mark
Setting up for third process...
Markings: Third mark
This is the fourth(default).
The twists are the 'hook' for the third process this is a method that calls a method in the higher class, this is useful to compose information before calling the method.
The next twist is the virtual method which can be overridden, this is pretty obviously for different functionality and similar to factory method.
A real example is the duplication of database 'setup' code (I'm guilty of this) the connection can be opened and the stream initialised (or not) and the SQL executed etc...
This has another specialized name - for auto-generated code which can be 'wrapped' in another class as the auto-generated codebase will keep altering and is called the Generation gap pattern.
Summary
Well that's the template method pattern, it's pretty simple, and can force a sequence for an algorithm.
It can also, help reduce code duplication as the duplicate code can be placed in the lower class.