Design Pattern Examples
Overview of object-oriented design patterns
program.c File Reference

Implementation of the main() entry point and the machinery to call all the design pattern example functions. See Main Overview. More...

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <version.h>
#include "helpers/enablevtmode.h"
#include "helpers/stringlist.h"
#include "Adapter_Exercise.h"
#include "Bridge_Exercise.h"
#include "Command_Exercise.h"
#include "Composite_Exercise.h"
#include "Decorator_Exercise.h"
#include "Facade_Exercise.h"
#include "Flyweight_Exercise.h"
#include "HandlerChain_Exercise.h"
#include "Interpreter_Exercise.h"
#include "Iterator_Exercise.h"
#include "Mediator_Exercise.h"
#include "Memento_Exercise.h"
#include "NullObject_Exercise.h"
#include "Observer_Exercise.h"
#include "Proxy_Exercise.h"
#include "State_Exercise.h"
#include "Strategy_Exercise.h"
#include "Visitor_Exercise.h"
Include dependency graph for program.c:

Go to the source code of this file.

Classes

struct  _Exercise
 Represents a single exercise or example for a design pattern. More...
 
struct  _Options
 Represents the command line options provided to the program, if any. More...
 

Typedefs

typedef void(* Action) (void)
 Alias for a function pointer, using C# as inspiration for the name.
 
typedef struct _Exercise Exercise
 Represents a single exercise or example for a design pattern.
 
typedef Exercise ExerciseList[]
 Alias for an array of Exercise objects.
 
typedef struct _Options Options
 Represents the command line options provided to the program, if any.
 

Functions

static void Help (ExerciseList exercises)
 Helper function to show usage information for this program.
 
static void ShowVersion (void)
 Helper function to show just the version of the application.
 
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. Displays help if requested and returns false.
 
int main (int argc, char **argv)
 Main entry point into this example program.
 

Variables

ExerciseList exercises
 

Detailed Description

Implementation of the main() entry point and the machinery to call all the design pattern example functions. See Main Overview.

Definition in file program.c.

Typedef Documentation

◆ Action

typedef void(* Action) (void)

Alias for a function pointer, using C# as inspiration for the name.

Definition at line 39 of file program.c.

◆ Exercise

typedef struct _Exercise Exercise

Represents a single exercise or example for a design pattern.

◆ ExerciseList

typedef Exercise ExerciseList[]

Alias for an array of Exercise objects.

Definition at line 60 of file program.c.

◆ Options

typedef struct _Options Options

Represents the command line options provided to the program, if any.

Function Documentation

◆ Help()

static void Help ( ExerciseList  exercises)
static

Helper function to show usage information for this program.

Parameters
exercisesList of Exercise objects to display if help is needed. The list is expected to be terminated by a NULL pointer for the exercise name.

Definition at line 82 of file program.c.

References exercises.

Referenced by ParseOptions().

◆ main()

int main ( int  argc,
char **  argv 
)

Main entry point into this example program.

Parameters
argcNumber of command line arguments + 1 (the first argument is the name of the program).
argvPointer to a list of zero-terminated strings containing the command line arguments.

Definition at line 206 of file program.c.

References enableVTMode(), _Options::exercise_names, exercises, main(), ParseOptions(), StringList_Clear(), StringList_Find(), and StringList_Initialize().

◆ ParseOptions()

static bool ParseOptions ( int  argc,
char **  argv,
Options options,
ExerciseList  exercises 
)
static

Helper function to parse the given options and store the results in the given Options structure. Displays help if requested and returns false.

Parameters
argcNumber of command line arguments + 1 (the first argument is the name of the program).
argvPointer to a list of zero-terminated strings containing the command line arguments.
optionsAn Options structure to be filled in by command line parameters.
exercisesList of Exercise objects to display if help is needed. The list is expected to be terminated by a NULL pointer for the exercise name.
Returns
true if the command lines were valid and help was not asked. Otherwise, a command line option was not valid or help was requested.

Definition at line 134 of file program.c.

References _Options::exercise_names, exercises, Help(), ShowVersion(), and StringList_AddString().

Referenced by main().

◆ ShowVersion()

static void ShowVersion ( void  )
static

Helper function to show just the version of the application.

Definition at line 111 of file program.c.

Referenced by ParseOptions().

Variable Documentation

◆ exercises

ExerciseList exercises
Initial value:
=
{
{"Adapter", Adapter_Exercise},
{"Bridge", Bridge_Exercise},
{"Command", Command_Exercise},
{"Composite", Composite_Exercise},
{"Decorator", Decorator_Exercise},
{"Facade", Facade_Exercise},
{"Flyweight", Flyweight_Exercise},
{"HandlerChain", HandlerChain_Exercise},
{"Interpreter", Interpreter_Exercise},
{"Iterator", Iterator_Exercise},
{"Mediator", Mediator_Exercise},
{"Memento", Memento_Exercise},
{"NullObject", NullObject_Exercise},
{"Observer", Observer_Exercise},
{"Proxy", Proxy_Exercise},
{"State", State_Exercise},
{"Strategy", Strategy_Exercise},
{"Visitor", Visitor_Exercise},
{NULL, NULL}}
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.
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 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.

Definition at line 177 of file program.c.

Referenced by Help(), Program::Help(), main(), ParseOptions(), Program::ParseOptions(), and Program::Run().