Design Pattern Examples
Overview of object-oriented design patterns
Strategy_ShowEntries_Class.h
Go to the documentation of this file.
1
5
6#pragma once
7#ifndef __STRATEGY_SHOWENTRIES_H__
8#define __STRATEGY_SHOWENTRIES_H__
9
10#include "helpers/formatstring.h"
11
13
15{
16
17
18 //########################################################################
19 //########################################################################
20
21
45 {
46 private:
51
59
60 public:
68 Strategy_ShowEntries_Class(SortOptions sortOption, bool reversedSort)
69 {
70 _reversedSort = reversedSort;
71 // Create the sorting strategy to use.
73
74 }
75
81 void ShowEntries(const std::vector<EntryInformation>& entries)
82 {
83 // Make a local copy of the entries so we don't disturb the original list.
84 std::vector<EntryInformation> localEntries(entries);
85 _sortEntries->Sort(localEntries);
86
87 // This is a tabular display, making it easier to follow the sorted data.
88 std::cout
89 << Helpers::formatstring(" Sort strategy: %s (order = %s)", _sortEntries->ToString().c_str(), _reversedSort ? "Descending" : "Ascending")
90 << std::endl;
91 std::cout
92 << Helpers::formatstring(" %6s %3s %3s", "Name", "Age", "Height")
93 << std::endl;
94 std::cout
95 << Helpers::formatstring(" %6s %3s %3s", "------", "---", "------")
96 << std::endl;
97 for(EntryInformation& entry : localEntries)
98 {
99 std::cout
100 << Helpers::formatstring(" %s", entry.ToString().c_str())
101 << std::endl;
102 }
103 }
104 };
105
106
107} // end namespace
108
109#endif // __STRATEGY_SHOWENTRIES_H__
110
static EntryInformation entries[]
List of individuals to play around with in the Strategy exercise.
Declaration of the Strategy_SortEntries_ClassFactory class and implementation of the various Strategy...
Represents a way of displaying a list of EntryInformation objects in a particular order....
ISortEntries::shared_ptr_t _sortEntries
The sorting strategy to use.
Strategy_ShowEntries_Class(SortOptions sortOption, bool reversedSort)
Constructor.
bool _reversedSort
Whether to reverse the normal order of the sort.
void ShowEntries(const std::vector< EntryInformation > &entries)
Display the specified entries in sorted order. The order of the sort was established when the Strateg...
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.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
Represents an individual with a Name, Age, and Height.
std::shared_ptr< ISortEntries > shared_ptr_t
Alias to make it easier to work with a shared pointer.