Design Pattern Examples
Overview of object-oriented design patterns
program.cpp
Go to the documentation of this file.
1
5
6#include <functional>
7#include <iostream>
8
9#include <version.h>
10
11#include "helpers/replace.h"
12#include "helpers/stringlist.h"
13#include "helpers/enablevtmode.h"
14
15#include "Adapter_Exercise.h"
16#include "Bridge_Exercise.h"
17#include "Command_Exercise.h"
18#include "Composite_Exercise.h"
19#include "Decorator_Exercise.h"
20#include "Facade_Exercise.h"
21#include "Flyweight_Exercise.h"
24#include "Iterator_Exercise.h"
25#include "Mediator_Exercise.h"
26#include "Memento_Exercise.h"
27#include "NullObject_Exercise.h"
28#include "Observer_Exercise.h"
29#include "Proxy_Exercise.h"
30#include "State_Exercise.h"
31#include "Strategy_Exercise.h"
32#include "Visitor_Exercise.h"
33
38{
39
43 typedef std::function<void()> Action;
44
48 class Program
49 {
50 private:
54 struct Exercise
55 {
59 std::string name;
60
65
66
72 Exercise(const char* nameOfExercise, Action exercise)
73 {
74 name = nameOfExercise;
75 exercise_to_run = exercise;
76 }
77 };
78
82 typedef std::vector<Exercise> ExerciseList;
83
87 struct Options
88 {
94 };
95
96 private:
103 {
104 std::string usage =
105 "{0} (v" APP_VERSION ") by Stephen P. Lepisto\n"
106 "usage: {0} [options] [exercise_name][[ exercise_name][...]]\n"
107 "\n"
108 "Runs through a series of exercises showing off design patterns. If no\n"
109 "exercise_name is given, then run through all exercises.\n"
110 "\n"
111 "Options:\n"
112 "--help, -?\n"
113 " This help text.\n"
114 "--version\n"
115 " Show just the version number of this application.\n"
116 "\n"
117 ""; // End of string.
118
119 std::string appName = "DesignPatternExamples_cpp";
120 std::cout << Helpers::Replace(usage, "{0}", appName.c_str());
121
122 std::cout << std::endl << "Exercises available:" << std::endl;
123 for (Exercise exercise : exercises)
124 {
125 std::cout << " " << exercise.name << std::endl;
126 }
127 }
128
133 {
134 std::cout << APP_VERSION << std::endl;
135 }
136
151 {
152 bool optionsValid = true;
153
154 options.exercise_names.clear();
155
156 if (args.size() > 0)
157 {
158 for (std::string argument : args)
159 {
160 if (argument == "--help" ||
161 argument == "-?" ||
162 argument == "/?")
163 {
165 optionsValid = false;
166 break;
167 }
168 else if (argument == "--version")
169 {
170 ShowVersion();
171 optionsValid = false;
172 break;
173 }
174 options.exercise_names.push_back(argument);
175 }
176 }
177
178 return optionsValid;
179 }
180
181 public:
186 void Run(StringList args)
187 {
189 {
190 // We use a lambda here that auto-binds to the "this" pointer ("[&]")
191 // for this object so as to avoid complicated declarations and
192 // bindings to class methods.
193 Exercise("Adapter", [&] { Adapter_Exercise(); }),
194 Exercise("Bridge", [&] { Bridge_Exercise(); }),
195 Exercise("Command", [&] { Command_Exercise(); }),
196 Exercise("Composite", [&] { Composite_Exercise(); }),
197 Exercise("Decorator", [&] { Decorator_Exercise(); }),
198 Exercise("Facade", [&] { Facade_Exercise(); }),
199 Exercise("Flyweight", [&] { Flyweight_Exercise(); }),
200 Exercise("HandlerChain", [&] { HandlerChain_Exercise(); }),
201 Exercise("Interpreter", [&] { Interpreter_Exercise(); }),
202 Exercise("Iterator", [&] { Iterator_Exercise(); }),
203 Exercise("Mediator", [&] { Mediator_Exercise(); }),
204 Exercise("Memento", [&] { Memento_Exercise(); }),
205 Exercise("NullObject", [&] { NullObject_Exercise(); }),
206 Exercise("Observer", [&] { Observer_Exercise(); }),
207 Exercise("Proxy", [&] { Proxy_Exercise(); }),
208 Exercise("State", [&] { State_Exercise(); }),
209 Exercise("Strategy", [&] { Strategy_Exercise(); }),
210 Exercise("Visitor", [&] { Visitor_Exercise(); }),
211 };
212
213 Options options;
214 if (ParseOptions(args, exercises, options))
215 {
216 for (Exercise exercise : exercises)
217 {
218 bool runExercise = options.exercise_names.empty();
219 if (!runExercise)
220 {
221 auto foundIter = std::find(
222 std::begin(options.exercise_names),
223 std::end(options.exercise_names),
224 exercise.name);
225 runExercise = (foundIter != std::end(options.exercise_names));
226 }
227 if (runExercise)
228 {
229 exercise.exercise_to_run();
230 }
231 }
232 }
233 }
234 };
235
236} // end namespace
237
238//########################################################################
239//########################################################################
240
241
249int main(int argc, char** argv)
250{
252
253 srand(static_cast<unsigned int>(time(nullptr)));
255 StringList args;
256 for (int argIndex = 1; argIndex < argc; ++argIndex)
257 {
258 args.push_back(std::string(argv[argIndex]));
259 }
260
261 prog.Run(args);
262
263 return EXIT_SUCCESS;
264}
Contains all the top-level Design Pattern Examples to match C#.
Definition: program.cpp:49
std::vector< Exercise > ExerciseList
Alias for a list of Exercise instances.
Definition: program.cpp:82
void Run(StringList args)
Run the specified examples.
Definition: program.cpp:186
void ShowVersion()
Helper function to show just the version of the application.
Definition: program.cpp:132
void Help(ExerciseList exercises)
Helper method to show usage information for this program.
Definition: program.cpp:102
bool ParseOptions(StringList args, ExerciseList exercises, Options &options)
Helper method to parse the given options and store the results in the given Options structure....
Definition: program.cpp:150
Declaration of the Adapter_Exercise() function as used in the Adapter Pattern.
Declaration of the Bridge_Exercise() function as used in the Bridge Pattern.
Declaration of the Command_Exercise() function as used in the Command Pattern.
Declaration of the Composite_Exercise() function as used in the Composite Pattern.
Declaration of the Decorator_Exercise() function as used in the Decorator Pattern.
Declaration of the Facade_Exercise() function as used in the Facade Pattern.
Declaration of the Flyweight_Exercise() function as used in the Flyweight Pattern.
Declaration of the HandlerChain_Exercise() function as used in the HandlerChain Pattern.
Declaration of the Interpreter_Exercise() function as used in the Interpreter Pattern.
Declaration of the Iterator_Exercise() function as used in the Iterator Pattern.
Declaration of the Mediator_Exercise() function as used in the Mediator Pattern.
Declaration of the Memento_Exercise() function as used in the Memento Pattern.
Declaration of the NullObject_Exercise() function as used in the Null Object Pattern.
Declaration of the Observer_Exercise() function as used in the Observer Pattern.
Declaration of the Proxy_Exercise() function as used in the Proxy Pattern.
Declaration of the State_Exercise() function as used in the State Pattern.
Declaration of the Strategy_Exercise() function as used in the Strategy Pattern.
Declaration of the Visitor_Exercise() function as used in the Visitor Pattern.
std::vector< std::string > StringList
Typedef for a vector of std::string.
The namespace containing all Design Pattern Examples implemented in C++.
void Bridge_Exercise()
Example of using the Bridge design pattern.
void Interpreter_Exercise()
Example of using the Interpreter design pattern.
void NullObject_Exercise()
Example of using the Null Object design pattern.
void Decorator_Exercise()
Example of using the Decorator design pattern.
void State_Exercise()
Example of using the State design pattern.
std::function< void()> Action
Alias for a function pointer, using C# as inspiration for the name.
Definition: program.cpp:43
void Iterator_Exercise()
Example of using the Iterator design pattern.
void Command_Exercise()
Example of using the Command design pattern.
void Strategy_Exercise()
Example of using the Strategy design pattern.
void Adapter_Exercise()
Example of using the Adapter Pattern.
void Memento_Exercise()
Example of using the Memento design pattern.
void HandlerChain_Exercise()
Example of using the Handle Chain or Chain of Responsibility design pattern.
void Flyweight_Exercise()
Example of using the Flyweight design pattern.
void Facade_Exercise()
Example of using the Facade design pattern.
void Proxy_Exercise()
Example of using the Proxy design pattern.
void Observer_Exercise()
Example of using the Observer design pattern.
void Composite_Exercise()
Example of using the Composite design pattern.
void Mediator_Exercise()
Example of using the Mediator design pattern.
void Visitor_Exercise()
Example of using the Visitor design pattern.
void enableVTMode()
On Windows, enable the virtual terminal processing mode on the Console's output handle....
std::string Replace(const std::string &s, const char *str1, const char *str2, bool bCaseInsensitive)
Replace all occurrences of narrow string str1 with narrow string str2 in s. If str2 is empty then all...
Definition: replace.cpp:47
ExerciseList exercises
Definition: program.c:177
int main(int argc, char **argv)
Main entry point into this example program.
Definition: program.cpp:249
Represents a single exercise or example for a design pattern.
Definition: program.cpp:55
Action exercise_to_run
Method to call to run the exercise.
Definition: program.cpp:64
Exercise(const char *nameOfExercise, Action exercise)
Constructor.
Definition: program.cpp:72
std::string name
Name of the exercise.
Definition: program.cpp:59
Represents the command line options provided to the program, if any.
Definition: program.cpp:88
StringList exercise_names
List of names of exercise to run. If this list is empty, run all exercises.
Definition: program.cpp:93