Design Pattern Examples
Overview of object-oriented design patterns
Strategy_Exercise.cpp
Go to the documentation of this file.
1
5
6#include <iostream>
7
8#include "Strategy_Exercise.h"
11
12
13namespace // Anonymous
14{
15 using namespace DesignPatternExamples_cpp;
16
20 std::vector<EntryInformation> entries =
21 {
22 // Name, age, height (in inches)
23 EntryInformation("Ronnie", 19, 84),
24 EntryInformation("Elaine", 29, 71),
25 EntryInformation("Jack", 20, 81),
26 EntryInformation("Myra", 35, 78),
27 EntryInformation("Fred", 18, 88),
28 };
29
30} // end namespace Anonmyous
31
32
34{
35
48 // ! [Using Strategy in C++]
50 {
51 std::cout << std::endl;
52 std::cout << "Strategy Exercise" << std::endl;
53
54 Strategy_ShowEntries_Class displaySortedByNameAscending(SortOptions::ByName, false);
55 displaySortedByNameAscending.ShowEntries(entries);
56
57 Strategy_ShowEntries_Class displaySortedByAgeAscending(SortOptions::ByAge, false);
58 displaySortedByAgeAscending.ShowEntries(entries);
59
60 Strategy_ShowEntries_Class displaySortedByHeightDescending(SortOptions::ByHeight, true);
61 displaySortedByHeightDescending.ShowEntries(entries);
62
63 std::cout << " Done." << std::endl;
64 }
65 // ! [Using Strategy in C++]
66
67} // end namespace
static EntryInformation entries[]
List of individuals to play around with in the Strategy exercise.
Implementation of the Strategy_ShowEntries_Class classes used in the Strategy Pattern.
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....
void ShowEntries(const std::vector< EntryInformation > &entries)
Display the specified entries in sorted order. The order of the sort was established when the Strateg...
Declaration of the Strategy_Exercise() function as used in the Strategy Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
@ ByHeight
Sort numerically by height in ascending order.
@ ByAge
Sort numerically by age in ascending order.
@ ByName
Sort alphabetically name in ascending order.
void Strategy_Exercise()
Example of using the Strategy design pattern.
Represents an individual with a Name, Age, and Height.