Design Pattern Examples
Overview of object-oriented design patterns
program.c
Go to the documentation of this file.
1
5
6#include <stdbool.h>
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <time.h>
11
12#include <version.h>
13
14#include "helpers/enablevtmode.h"
15#include "helpers/stringlist.h"
16
17#include "Adapter_Exercise.h"
18#include "Bridge_Exercise.h"
19#include "Command_Exercise.h"
20#include "Composite_Exercise.h"
21#include "Decorator_Exercise.h"
22#include "Facade_Exercise.h"
23#include "Flyweight_Exercise.h"
26#include "Iterator_Exercise.h"
27#include "Mediator_Exercise.h"
28#include "Memento_Exercise.h"
29#include "NullObject_Exercise.h"
30#include "Observer_Exercise.h"
31#include "Proxy_Exercise.h"
32#include "State_Exercise.h"
33#include "Strategy_Exercise.h"
34#include "Visitor_Exercise.h"
35
39typedef void(*Action)(void);
40
44typedef struct _Exercise
45{
49 const char* name;
50
56
61
62
66typedef struct _Options
67{
74
75
83{
84 const char* usage =
85 "DesignPatternExamples_c (v" APP_VERSION ") by Stephen P. Lepisto\n"
86 "usage: DesignPatternExamples_c [options] [exercise_name][[ exercise_name][...]]\n"
87 "\n"
88 "Runs through a series of exercises showing off design patterns. If no\n"
89 "exercise_name is given, then run through all exercises.\n"
90 "\n"
91 "Options:\n"
92 "--help, -?\n"
93 " This help text.\n"
94 "--version\n"
95 " Show just the version number of this application.\n"
96 "\n"
97 ""; // End of string.
98
99 printf("%s\n", usage);
100
101 printf("Exercises available:\n");
102 for (size_t index = 0; exercises[index].name != NULL; index++)
103 {
104 printf(" %s\n", exercises[index].name);
105 }
106}
107
111static void ShowVersion(void)
112{
113 printf("%s\n", APP_VERSION);
114}
115
116
134static bool ParseOptions(int argc, char** argv, Options* options, ExerciseList exercises)
135{
136 bool optionsValid = true;
137
138 if (options == NULL)
139 {
140 printf("Error! ParseOptions() requires non-NULL pointer to the Options structure.\n");
141 return false;
142 }
143
144 if (argc > 1)
145 {
146 for (int index = 1; index < argc; index++)
147 {
148 if (strcmp(argv[index], "--help") == 0 ||
149 strcmp(argv[index], "-?") == 0 ||
150 strcmp(argv[index], "/?") == 0)
151 {
153 optionsValid = false;
154 break;
155 }
156 if (strcmp(argv[index], "--version") == 0)
157 {
158 ShowVersion();
159 optionsValid = false;
160 break;
161 }
162 if (!StringList_AddString(&options->exercise_names, argv[index]))
163 {
164 printf("Error! Out of memory storing exercise name in the Options structure.");
165 return false;
166 }
167 }
168 }
169
170 return optionsValid;
171}
172
173
174//########################################################################
175//########################################################################
176
178 {
179 {"Adapter", Adapter_Exercise},
180 {"Bridge", Bridge_Exercise},
181 {"Command", Command_Exercise},
182 {"Composite", Composite_Exercise},
183 {"Decorator", Decorator_Exercise},
184 {"Facade", Facade_Exercise},
185 {"Flyweight", Flyweight_Exercise},
186 {"HandlerChain", HandlerChain_Exercise},
187 {"Interpreter", Interpreter_Exercise},
188 {"Iterator", Iterator_Exercise},
189 {"Mediator", Mediator_Exercise},
190 {"Memento", Memento_Exercise},
191 {"NullObject", NullObject_Exercise},
192 {"Observer", Observer_Exercise},
193 {"Proxy", Proxy_Exercise},
194 {"State", State_Exercise},
195 {"Strategy", Strategy_Exercise},
196 {"Visitor", Visitor_Exercise},
197 {NULL, NULL}};
198
206int main(int argc, char** argv)
207{
208 enableVTMode();
209 srand((unsigned int)(time(NULL)));
210 Options options = { 0 };
212
213 if (ParseOptions(argc, argv, &options, exercises))
214 {
215 for (int index = 0; exercises[index].name != NULL; index++)
216 {
217 bool runExercise = (options.exercise_names.strings_count == 0);
218 if (!runExercise)
219 {
220 runExercise = (StringList_Find(&options.exercise_names, exercises[index].name) != -1);
221 }
222 if (runExercise)
223 {
224 exercises[index].exercise_to_run();
225 }
226 }
227 }
229
230 return EXIT_SUCCESS;
231}
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.
void enableVTMode(void)
Enables the virtual terminal processing mode on the current Windows Console. When the program ends,...
Definition: enablevtmode.c:39
std::vector< std::string > StringList
Typedef for a vector of std::string.
struct _Exercise Exercise
Represents a single exercise or example for a design pattern.
ExerciseList exercises
Definition: program.c:177
Exercise ExerciseList[]
Alias for an array of Exercise objects.
Definition: program.c:60
static void Help(ExerciseList exercises)
Helper function to show usage information for this program.
Definition: program.c:82
struct _Options Options
Represents the command line options provided to the program, if any.
static void ShowVersion(void)
Helper function to show just the version of the application.
Definition: program.c:111
void(* Action)(void)
Alias for a function pointer, using C# as inspiration for the name.
Definition: program.c:39
static bool ParseOptions(int argc, char **argv, Options *options, ExerciseList exercises)
Helper function to parse the given options and store the results in the given Options structure....
Definition: program.c:134
int main(int argc, char **argv)
Main entry point into this example program.
Definition: program.cpp:249
void StringList_Clear(StringList *stringList)
Clear the specified string list. All strings in the list are released. The string list can then be us...
Definition: stringlist.c:30
int StringList_Find(StringList *stringList, const char *string)
Search the given string list for the given string. If found, return the index of the found string.
Definition: stringlist.c:157
void StringList_Initialize(StringList *stringList)
Initialize the given string list.
Definition: stringlist.c:17
bool StringList_AddString(StringList *stringList, const char *string)
Add a string to the given string list.
Definition: stringlist.c:49
Represents a single exercise or example for a design pattern.
Definition: program.c:45
Action exercise_to_run
Function to call to run the exercise.
Definition: program.c:54
const char * name
Name of the exercise.
Definition: program.c:49
Represents the command line options provided to the program, if any.
Definition: program.c:67
StringList exercise_names
List of names of exercise to run. If this list is empty, run all exercises.
Definition: program.c:72