This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law. .NET 1.1+

# Template Method Design Pattern

The template method pattern is a design pattern that allows a group of interchangeable, similarly structured, multi-step algorithms to be defined. Each algorithm follows the same series of actions but provides a different implementation of the steps.

## Example Template Method

In this section we will create the example scoring system described above using the template method design pattern. The base class will define three steps. The first step will calculate a score based upon the number of targets successfully hit. The second step will calculate the reduction in score, should there be one, based upon the time taken to complete the circuit. The final step will combine the results of the first two steps to generate an overall score. The template method will call the three steps in order, returning the score generated by the third step.

The scoring rules for different types of players are as follows:

• Men. One hundred points will be awarded for every target hit. Five points will be subtracted for each second of time taken.
• Women. One hundred points will be awarded for every target hit. Four points will be subtracted for each second of time taken.
• Children. Two hundred points will be awarded for every target hit. Two points will be subtracted for each second of time taken. Negative scores are replaced with a zero score.

We can now define the base class and the three algorithms using the following code:

```public abstract class ScoringAlgorithm
{
public int GenerateScore(int hits, TimeSpan time)
{
int score = CalculateBaseScore(hits);
int reduction = CalculateReduction(time);
return CalculateOverallScore(score, reduction);
}

public abstract int CalculateBaseScore(int hits);

public abstract int CalculateReduction(TimeSpan time);

public abstract int CalculateOverallScore(int score, int reduction);
}

public class MensScoringAlgorithm : ScoringAlgorithm
{
public override int CalculateBaseScore(int hits)
{
return hits * 100;
}

public override int CalculateReduction(TimeSpan time)
{
return ((int)time.TotalSeconds / 5);
}

public override int CalculateOverallScore(int score, int reduction)
{
return score - reduction;
}
}

public class WomensScoringAlgorithm : ScoringAlgorithm
{
public override int CalculateBaseScore(int hits)
{
return hits * 100;
}

public override int CalculateReduction(TimeSpan time)
{
return ((int)time.TotalSeconds / 4);
}

public override int CalculateOverallScore(int score, int reduction)
{
return score - reduction;
}
}

public class ChildrensScoringAlgorithm : ScoringAlgorithm
{
public override int CalculateBaseScore(int hits)
{
return hits * 200;
}

public override int CalculateReduction(TimeSpan time)
{
return ((int)time.TotalSeconds / 2);
}

public override int CalculateOverallScore(int score, int reduction)
{
if (score > reduction)
return score - reduction;
else
return 0;
}
}```

### Testing the Template Method

To test the example template method we can run each variation of the algorithm to generate a score for men, women or children. Create the following code in a console application and execute it to see the results.

```ScoringAlgorithm algorithm;

Console.Write("Man ");
algorithm = new MensScoringAlgorithm();
Console.WriteLine(algorithm.GenerateScore(8, new TimeSpan(0, 1, 31)));

Console.Write("Woman ");
algorithm = new WomensScoringAlgorithm();
Console.WriteLine(algorithm.GenerateScore(9, new TimeSpan(0, 1, 49)));

Console.Write("Child ");
algorithm = new ChildrensScoringAlgorithm();
Console.WriteLine(algorithm.GenerateScore(5, new TimeSpan(0, 3, 2)));

/* OUTPUT

Man 782
Woman 873
Child 909

*/```
19 July 2009