BlackWaspTM

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

Design Patterns
.NET 1.1+

Strategy Design Pattern

The strategy pattern is a design pattern that allows a set of similar algorithms to be defined and encapsulated in their own classes. The algorithm to be used for a particular purpose may then be selected at run-time according to your requirements.

Testing the Strategy

To test the strategy example classes we can use a console application containing all of the above code. Add the code shown below to the Main method and then execute the program. The test code creates a new scoreboard and displays three scores. Each score using a different scoring algorithm.

Scoreboard board = new Scoreboard();

Console.Write("Man ");
board.Scoring = new MensScoringAlgorithm();
board.ShowScore(8, new TimeSpan(0, 1, 31));

Console.Write("Woman ");
board.Scoring = new WomensScoringAlgorithm();
board.ShowScore(9, new TimeSpan(0, 1, 49));

Console.Write("Child ");
board.Scoring = new ChildrensScoringAlgorithm();
board.ShowScore(5, new TimeSpan(0, 3, 2));

/* OUTPUT

Man 782
Woman 873
Child 909

*/
4 May 2009