Design Pattern Examples
Overview of object-oriented design patterns
Strategy_ShowEntries_Class.cs
Go to the documentation of this file.
1
6
7using System;
8using System.Collections.Generic;
9
11{
15 public class EntryInformation
16 {
20 public string Name;
21
25 public int Age;
26
30 public int Height;
31
38 public EntryInformation(string name, int age, int height)
39 {
40 Name = name;
41 Age = age;
42 Height = height;
43 }
44
45
51 public override string ToString()
52 {
53 return String.Format("{0,6} {1,3} {2,3}\"", Name, Age, Height);
54 }
55 }
56
57
58 //########################################################################
59 //########################################################################
60
61
85 {
89 public enum SortOptions
90 {
94 ByName,
95
99 ByAge,
100
104 ByHeight,
105 }
106
107
112
120
128 public Strategy_ShowEntries_Class(SortOptions sortOption, bool reversedSort)
129 {
130 _reversedSort = reversedSort;
131 // Create the sorting strategy to use.
132 _sortEntries = Strategy_SortEntries_ClassFactory.Create(sortOption, reversedSort);
133
134 }
135
142 {
143 // Make a local copy of the entries so we can sort them using the
144 // List class's sorting capabilities; otherwise, we would have to
145 // write our own sorting algorithms to work on an array.
146 List<EntryInformation> localEntries = new List<EntryInformation>(entries);
147 _sortEntries.Sort(localEntries);
148
149 // This is a tabular display, making it easier to follow the sorted data.
150 Console.WriteLine(" Sort strategy: {0} (order = {1})", _sortEntries, _reversedSort ? "Descending" : "Ascending");
151 Console.WriteLine(" {0,6} {1,3} {2,3}", "Name", "Age", "Height");
152 Console.WriteLine(" {0,6} {1,3} {2,3}", "------", "---", "------");
153 foreach(EntryInformation entry in localEntries)
154 {
155 Console.WriteLine(" {0}", entry);
156 }
157 }
158 }
159}
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.
override string ToString()
Convert the entry into a string. In this case, a formatted string where name, age,...
int Height
Height of this individual, in inches.
EntryInformation(string name, int age, int height)
Constructor.
Represents a way of displaying a list of EntryInformation objects in a particular order....
Strategy_ShowEntries_Class(SortOptions sortOption, bool reversedSort)
Constructor.
bool _reversedSort
Whether to reverse the normal order of the sort.
SortOptions
Identifies the different sorting strategies available.
@ ByAge
Sort numerically by age in ascending order.
@ ByName
Sort alphabetically name in ascending order.
@ ByHeight
Sort numerically by height in ascending order.
void ShowEntries(EntryInformation[] entries)
Display the specified entries in sorted order. The order of the sort was established when the Strateg...
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#.