Design Pattern Examples
Overview of object-oriented design patterns
Strategy_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7
9{
22 internal class Strategy_Exercise
23 {
28 {
29 // Name, age, height (in inches)
30 new EntryInformation("Ronnie", 19, 84),
31 new EntryInformation("Elaine", 29, 71),
32 new EntryInformation("Jack", 20, 81),
33 new EntryInformation("Myra", 35, 78),
34 new EntryInformation("Fred", 18, 88),
35 };
36
40 // ! [Using Strategy in C#]
41 public void Run()
42 {
43 Console.WriteLine();
44 Console.WriteLine("Strategy Exercise");
45
46 Strategy_ShowEntries_Class displaySortedByNameAscending;
47 displaySortedByNameAscending = new Strategy_ShowEntries_Class(Strategy_ShowEntries_Class.SortOptions.ByName, false);
48 displaySortedByNameAscending.ShowEntries(entries);
49
50 Strategy_ShowEntries_Class displaySortedByAgeAscending;
51 displaySortedByAgeAscending = new Strategy_ShowEntries_Class(Strategy_ShowEntries_Class.SortOptions.ByAge, false);
52 displaySortedByAgeAscending.ShowEntries(entries);
53
54 Strategy_ShowEntries_Class displaySortedByHeightDescending;
55 displaySortedByHeightDescending = new Strategy_ShowEntries_Class(Strategy_ShowEntries_Class.SortOptions.ByHeight, true);
56 displaySortedByHeightDescending.ShowEntries(entries);
57
58 Console.WriteLine(" Done.");
59 }
60 // ! [Using Strategy in C#]
61 }
62}
Represents an individual with a Name, Age, and Height.
Example of using the Strategy Pattern in C#.
void Run()
Executes the example for the Strategy Pattern in C#.
EntryInformation[] entries
List of individuals to play around with in the Strategy exercise.
Represents a way of displaying a list of EntryInformation objects in a particular order....
SortOptions
Identifies the different sorting strategies available.
void ShowEntries(EntryInformation[] entries)
Display the specified entries in sorted order. The order of the sort was established when the Strateg...
The namespace containing all Design Pattern Examples implemented in C#.