Design Pattern Examples
Overview of object-oriented design patterns
Strategy_SortEntries_Classes.cpp
Go to the documentation of this file.
1
5
6#include <stdexcept>
7
8#include "helpers/formatstring.h"
9
11
12namespace // Anonymous
13{
14 using namespace DesignPatternExamples_cpp;
15
21 std::string _SortOptionToString(SortOptions sortOption)
22 {
23 std::string optionAsString;
24
25 switch (sortOption)
26 {
27 case SortOptions::ByName:
28 optionAsString = "ByName";
29 break;
30
31 case SortOptions::ByAge:
32 optionAsString = "ByAge";
33 break;
34
35 case SortOptions::ByHeight:
36 optionAsString = "ByHeight";
37 break;
38
39 default:
40 optionAsString = Helpers::formatstring("Unknown %d", static_cast<int>(sortOption));
41 break;
42 }
43 return optionAsString;
44 }
45
46
47} // end anonymous namespace
48
49
52
53
55{
56
58 {
60
61 switch (sortOption)
62 {
64 sortEntries = std::make_shared<Strategy_SortEntries_ByName>(reversedSort);
65 break;
66
68 sortEntries = std::make_shared < Strategy_SortEntries_ByAge>(reversedSort);
69 break;
70
72 sortEntries = std::make_shared < Strategy_SortEntries_ByHeight>(reversedSort);
73 break;
74
75 default:
76 {
77 std::string message = Helpers::formatstring("Unrecognized sort option: %s", _SortOptionToString(sortOption).c_str());
78 throw std::runtime_error(message.c_str());
79 }
80 }
81
82 return sortEntries;
83 }
84
85} // end namespace
Declaration of the Strategy_SortEntries_ClassFactory class and implementation of the various Strategy...
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.
@ ByHeight
Sort numerically by height in ascending order.
@ ByAge
Sort numerically by age in ascending order.
@ ByName
Sort alphabetically name in ascending order.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::shared_ptr< ISortEntries > shared_ptr_t
Alias to make it easier to work with a shared pointer.