Design Pattern Examples
Overview of object-oriented design patterns
Strategy_SortEntries_Classes.h
Go to the documentation of this file.
1
8
9#pragma once
10#ifndef __STRATEGY_SORTENTRIES_CLASSES_H__
11#define __STRATEGY_SORTENTRIES_CLASSES_H__
12
13#include <algorithm>
14#include <exception>
15
18
20{
21
22
27 {
28 private:
33
34 public:
40 Strategy_SortEntries_ByName(bool reversedSort)
41 : _reversedSort(reversedSort)
42 {
43 }
44
45
55 void Sort(std::vector<EntryInformation>& entries)
56 {
57 // C++ lambda can "import" only local variables so copy the class
58 // variable to a local variable.
59 bool reversedSort = _reversedSort;
60
61 // Take advantage of C++'s lambda/anonymous function capability
62 // to specify the sorting criteria so we don't have to define a
63 // separate method for this. This is where lambdas really shine.
64 std::sort(std::begin(entries), std::end(entries),
65 [reversedSort](EntryInformation& first, EntryInformation& second)
66 {
67 return reversedSort ? (first.Name > second.Name) : (first.Name < second.Name);
68 });
69 }
70
71
72 std::string ToString()
73 {
74 std::string function(__FUNCTION__);
75 return function.substr(0, function.rfind("::"));
76 }
77 };
78
79
80 //########################################################################
81 //########################################################################
82
83
88 {
89 private:
94
95 public:
101 Strategy_SortEntries_ByAge(bool reversedSort)
102 : _reversedSort(reversedSort)
103 {
104 }
105
106
116 void Sort(std::vector<EntryInformation>& entries)
117 {
118 // C++ lambda can "import" only local variables so copy the class
119 // variable to a local variable.
120 bool reversedSort = _reversedSort;
121
122 // Take advantage of C++'s lambda/anonymous function capability
123 // to specify the sorting criteria so we don't have to define a
124 // separate method for this. This is where lambdas really shine.
125 std::sort(std::begin(entries), std::end(entries),
126 [reversedSort](EntryInformation& first, EntryInformation& second)
127 {
128 return reversedSort ? (first.Age > second.Age) : (first.Age < second.Age);
129 });
130 }
131
132
133 std::string ToString()
134 {
135 std::string function(__FUNCTION__);
136 return function.substr(0, function.rfind("::"));
137 }
138 };
139
140
141 //########################################################################
142 //########################################################################
143
144
149 {
150 private:
155
156 public:
163 : _reversedSort(reversedSort)
164 {
165 }
166
167
177 void Sort(std::vector<EntryInformation>& entries)
178 {
179 // C++ lambda can "import" only local variables so copy the class
180 // variable to a local variable.
181 bool reversedSort = _reversedSort;
182
183 // Take advantage of C++'s lambda/anonymous function capability
184 // to specify the sorting criteria so we don't have to define a
185 // separate method for this. This is where lambdas really shine.
186 std::sort(std::begin(entries), std::end(entries),
187 [reversedSort](EntryInformation& first, EntryInformation& second)
188 {
189 return reversedSort ? (first.Height > second.Height) : (first.Height < second.Height);
190 });
191 }
192
193
194 std::string ToString()
195 {
196 std::string function(__FUNCTION__);
197 return function.substr(0,function.rfind("::"));
198 }
199 };
200
201
206 {
207 public:
220 static ISortEntries::shared_ptr_t Create(SortOptions sortOption, bool reversedSort);
221 };
222
223} // end namespace
224
225#endif // __STRATEGY_SORTENTRIES_CLASSES_H__
226
Implementation of the EntryInformation class as used for data in the Strategy Pattern.
static EntryInformation entries[]
List of individuals to play around with in the Strategy exercise.
Declaration of the ISortEntries interface as used in the Strategy Pattern.
Strategy for sorting the ages in ascending (or descending) order.
bool _reversedSort
Controls order of sort: true for descending, false for ascending.
void Sort(std::vector< EntryInformation > &entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
std::string ToString()
Return a string representation of the sorting strategy.
Strategy for sorting the heights in ascending (or descending) order.
bool _reversedSort
Controls order of sort: true for descending, false for ascending.
void Sort(std::vector< EntryInformation > &entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
std::string ToString()
Return a string representation of the sorting strategy.
Strategy for sorting the names in ascending (or descending) order.
bool _reversedSort
Controls order of sort: true for descending, false for ascending.
void Sort(std::vector< EntryInformation > &entries)
Sort the specified list of entries in place. A list is used here so we can leverage the List's sortin...
std::string ToString()
Return a string representation of the sorting strategy.
Holds the class constructor for the sorting strategies.
static ISortEntries::shared_ptr_t Create(SortOptions sortOption, bool reversedSort)
Generate an instance of a sorting strategy based on the given sorting option and reversed sort flag....
The namespace containing all Design Pattern Examples implemented in C++.
SortOptions
Identifies the different sorting strategies available.
Represents an individual with a Name, Age, and Height.
int Age
Age of this individual, in years.
int Height
Height of this individual, in inches.
std::string Name
Name of this individual.
std::shared_ptr< ISortEntries > shared_ptr_t
Alias to make it easier to work with a shared pointer.