Design Pattern Examples
Overview of object-oriented design patterns
Strategy_SortEntries_Classes.cs
Go to the documentation of this file.
1
6
7// The Strategy pattern is used when a choice of multiple different but
8// related algorithms can be made at startup time. The implementation of each
9// related algorithm exposes the same interface so as to hide the details of
10// implementation from the calling program.
11//
12// This module holds the sorting strategies used on a list of EntryInformation
13// objects.
14
15using System;
16using System.Collections.Generic;
17
19{
23 internal interface ISortEntries
24 {
31 void Sort(List<EntryInformation> entries);
32 }
33
34
35 //########################################################################
36 //########################################################################
37
38
43 {
48
54 public Strategy_SortEntries_ByName(bool reversedSort)
55 {
56 _reversedSort = reversedSort;
57 }
58
59
69 public void Sort(List<EntryInformation> entries)
70 {
71 // Take advantage of C#'s lambda/anonymous function capability
72 // to specify the sorting criteria so we don't have to define a
73 // separate method for this. This is where lambdas really shine.
74 entries.Sort(delegate (EntryInformation left, EntryInformation right)
75 {
76 return (_reversedSort) ? right.Name.CompareTo(left.Name) : left.Name.CompareTo(right.Name);
77 });
78 }
79 }
80
81
82 //########################################################################
83 //########################################################################
84
85
90 {
95
101 public Strategy_SortEntries_ByAge(bool reversedSort)
102 {
103 _reversedSort = reversedSort;
104 }
105
106
116 public void Sort(List<EntryInformation> entries)
117 {
118 // Take advantage of C#'s lambda/anonymous function capability
119 // to specify the sorting criteria so we don't have to define a
120 // separate method for this. This is where lambdas really shine.
121 entries.Sort(delegate (EntryInformation left, EntryInformation right)
122 {
123 return (_reversedSort) ? right.Age.CompareTo(left.Age) : left.Age.CompareTo(right.Age);
124 });
125 }
126 }
127
128
129 //########################################################################
130 //########################################################################
131
132
137 {
142
148 public Strategy_SortEntries_ByHeight(bool reversedSort)
149 {
150 _reversedSort = reversedSort;
151 }
152
153
163 public void Sort(List<EntryInformation> entries)
164 {
165 // Take advantage of C#'s lambda/anonymous function capability
166 // to specify the sorting criteria so we don't have to define a
167 // separate method for this. This is where lambdas really shine.
168 entries.Sort(delegate (EntryInformation left, EntryInformation right)
169 {
170 return (_reversedSort) ? right.Height.CompareTo(left.Height) : left.Height.CompareTo(right.Height);
171 });
172 }
173 }
174
175
180 {
193 internal static ISortEntries Create(Strategy_ShowEntries_Class.SortOptions sortOption, bool reversedSort)
194 {
195 ISortEntries sortEntries;
196
197 switch(sortOption)
198 {
200 sortEntries = new Strategy_SortEntries_ByName(reversedSort);
201 break;
202
204 sortEntries = new Strategy_SortEntries_ByAge(reversedSort);
205 break;
206
208 sortEntries = new Strategy_SortEntries_ByHeight(reversedSort);
209 break;
210
211 default:
212 {
213 string message = string.Format("Unrecognized sort option: {0}", sortOption);
214 throw new ApplicationException(message);
215 }
216 }
217
218 return sortEntries;
219 }
220 }
221}
static EntryInformation entries[]
List of individuals to play around with in the Strategy exercise.
Represents an individual with a Name, Age, and Height.
int Age
Age of this individual, in years.
string Name
Name of this individual.
int Height
Height of this individual, in inches.
Represents a way of displaying a list of EntryInformation objects in a particular order....
SortOptions
Identifies the different sorting strategies available.
Strategy for sorting the ages in ascending (or descending) order.
bool _reversedSort
Controls order of sort: true for descending, false for ascending.
void Sort(List< EntryInformation > entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
Strategy for sorting the heights in ascending (or descending) order.
bool _reversedSort
Controls order of sort: true for descending, false for ascending.
void Sort(List< EntryInformation > entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
Strategy for sorting the names in ascending (or descending) order.
bool _reversedSort
Controls order of sort: true for descending, false for ascending.
void Sort(List< EntryInformation > entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
static ISortEntries Create(Strategy_ShowEntries_Class.SortOptions sortOption, bool reversedSort)
Generate an instance of a sorting strategy based on the given sorting option and reversed sort flag....
void Sort(List< EntryInformation > entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
The namespace containing all Design Pattern Examples implemented in C#.