Design Pattern Examples
Overview of object-oriented design patterns
Program.cs
Go to the documentation of this file.
1
6
7using System;
8using System.Collections.Generic;
9using System.Reflection;
10
15{
19 class Program
20 {
21 //########################################################################
22
26 struct Exercise
27 {
31 public string name;
32
37
38
44 public Exercise(string nameOfExercise, Action exercise)
45 {
46 name = nameOfExercise;
47 exercise_to_run = exercise;
48 }
49 }
50
54 internal struct Options
55 {
60 public List<string> exercise_names;
61 }
62
63 //########################################################################
64
69 private string GetVersion()
70 {
71 string version = string.Empty;
72 Version? asm_version = typeof(Program).Assembly.GetName().Version;
73 if (asm_version != null)
74 {
75 version = asm_version.ToString();
76 }
77 return version;
78 }
79
84 private void Help(Exercise[] exercises)
85 {
86 string usage =
87 "{0} (v{1}) by Stephen P. Lepisto\n" +
88 "usage: {0} [--help][-?][options] [exercise_name][[ exercise_name][...]]\n" +
89 "\n" +
90 "Runs through a series of exercises showing off design patterns. If no exercise_name\n" +
91 "is given, then run through all exercises.\n" +
92 "\n" +
93 "Options:\n" +
94 "--help, -?\n" +
95 " This help text.\n" +
96 "--version\n" +
97 " Show just the version number of this application.\n" +
98 "\n" +
99 ""; // End of string.
100
101 string appName = System.IO.Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
102 Console.Write(usage, appName, GetVersion());
103
104 Console.WriteLine("\nExercises available:");
105 foreach (Exercise exercise in exercises)
106 {
107 Console.WriteLine(" {0}", exercise.name);
108 }
109 }
110
114 private void ShowVersion()
115 {
116 Console.WriteLine(GetVersion());
117 }
118
129 bool ParseOptions(string[] args, Exercise[] exercises, ref Options options)
130 {
131 bool optionsValid = true;
132
133 options.exercise_names = new List<string>();
134
135 if (args.Length > 0)
136 {
137 foreach (string argument in args)
138 {
139 string arg = argument;
140 if (string.Compare(arg, "--help") == 0 ||
141 string.Compare(arg, "-?") == 0 ||
142 string.Compare(arg, "/?") == 0)
143 {
145 optionsValid = false;
146 break;
147 }
148 else if (string.Compare(arg, "--version") == 0)
149 {
150 ShowVersion();
151 optionsValid = false;
152 break;
153 }
154 options.exercise_names.Add(arg);
155 }
156 }
157
158 return optionsValid;
159 }
160
161
166 void Run(string[] args)
167 {
168 Exercise[] exercises = new Exercise[]
169 {
170 new Exercise("Adapter", () => { Adapter_Exercise exercise = new Adapter_Exercise(); exercise.Run(); }),
171 new Exercise("Bridge", () => { Bridge_Exercise exercise = new Bridge_Exercise(); exercise.Run(); }),
172 new Exercise("Command", () => { Command_Exercise exercise = new Command_Exercise(); exercise.Run(); }),
173 new Exercise("Composite", () => { Composite_Exercise exercise = new Composite_Exercise(); exercise.Run(); }),
174 new Exercise("Decorator", () => { Decorator_Exercise exercise = new Decorator_Exercise(); exercise.Run(); }),
175 new Exercise("Facade", () => { Facade_Exercise exercise = new Facade_Exercise(); exercise.Run(); }),
176 new Exercise("Flyweight", () => { Flyweight_Exercise exercise = new Flyweight_Exercise(); exercise.Run(); }),
177 new Exercise("HandlerChain", () => { HandlerChain_Exercise exercise = new HandlerChain_Exercise(); exercise.Run(); }),
178 new Exercise("Interpreter", () => { Interpreter_Exercise exercise = new Interpreter_Exercise(); exercise.Run(); }),
179 new Exercise("Iterator", () => { Iterator_Exercise exercise = new Iterator_Exercise(); exercise.Run(); }),
180 new Exercise("Mediator", () => { Mediator_Exercise exercise = new Mediator_Exercise(); exercise.Run(); }),
181 new Exercise("Memento", () => { Memento_Exercise exercise = new Memento_Exercise(); exercise.Run(); }),
182 new Exercise("NullObject", () => { NullObject_Exercise exercise = new NullObject_Exercise(); exercise.Run(); }),
183 new Exercise("Observer", () => { Observer_Exercise exercise = new Observer_Exercise(); exercise.Run(); }),
184 new Exercise("Proxy", () => { Proxy_Exercise exercise = new Proxy_Exercise(); exercise.Run(); }),
185 new Exercise("State", () => { State_Exercise exercise = new State_Exercise(); exercise.Run(); }),
186 new Exercise("Strategy", () => { Strategy_Exercise exercise = new Strategy_Exercise(); exercise.Run(); }),
187 new Exercise("Visitor", () => { Visitor_Exercise exercise = new Visitor_Exercise(); exercise.Run(); }),
188 };
189
190 Options options = new Options();
191 if (ParseOptions(args, exercises, ref options))
192 {
193 foreach (Exercise exercise in exercises)
194 {
195 if (options.exercise_names.Count == 0 ||
196 options.exercise_names.FindIndex((string option_name) =>
197 String.Compare(option_name, exercise.name, true) == 0) != -1)
198 {
199 exercise.exercise_to_run();
200 }
201 }
202 }
203 }
204
205
206 //########################################################################
207 //########################################################################
208
213 static void Main(string[] args)
214 {
215 // Enable the virtual terminal processing mode for the life of this
216 // program.
218 {
219 Program prog = new Program();
220 prog.Run(args);
221 }
222 }
223 }
224}
Example of using the Adapter Pattern in C#.
void Run()
Executes the example for the Adapter Pattern in C#.
Example of using the Bridge Pattern in C#.
void Run()
Executes the example for the Bridge Pattern in C#.
Example of using the Command Pattern in C#.
void Run()
Executes the example for the Command Pattern in C#.
Example of using the Composite Pattern in C#.
void Run()
Executes the example for the Composite Pattern in C#.
Example of using the Decorator Pattern in C#.
void Run()
Executes the example for the Decorator Pattern in C#.
Enables the virtual terminal processing mode on the current Windows Console. When the program ends,...
Example of using the Facade Pattern in C#.
void Run()
Executes the example for the Facade Pattern in C#.
Example of using the Flyweight Pattern in C#.
void Run()
Executes the example for the Flyweight Pattern in C#.
Example of using the HandlerChain Pattern or Chain of Responsibility pattern in C#.
void Run()
Executes the example for the HandlerChain Pattern in C#.
Example of using the Interpreter Pattern in C#.
void Run()
Executes the example for the Interpreter Pattern in C#.
Example of using the Iterator Pattern in C#.
void Run()
Executes the example for the Iterator Pattern in C#.
Example of using the Mediator Pattern in C#.
void Run()
Executes the example for the Mediator Pattern in C#.
Example of using the Memento Pattern in C#.
void Run()
Executes the example for the Memento Pattern in C#.
Example of using the Null Object Pattern in C#.
void Run()
Executes the example for the Null Object Pattern in C#.
Example of using the Observer Pattern in C#.
void Run()
Executes the example for the Observer Pattern in C#.
Contains all the top-level Design Pattern Examples.
Definition: Program.cs:20
static void Main(string[] args)
Main entry point into this example program.
Definition: Program.cs:213
string GetVersion()
Helper method to get the version of this assembly.
Definition: Program.cs:69
bool ParseOptions(string[] args, Exercise[] exercises, ref Options options)
Helper method to parse the given options and store the results in the given Options structure....
Definition: Program.cs:129
void Run(string[] args)
Run the specified examples.
Definition: Program.cs:166
void Help(Exercise[] exercises)
Helper method to show usage information for this program.
Definition: Program.cs:84
void ShowVersion()
Helper method to show just the version of the application.
Definition: Program.cs:114
Example of using the Proxy Pattern in C#.
void Run()
Executes the example for the Proxy Pattern in C#.
Example of using the State Pattern in C#.
void Run()
Executes the example for the State Pattern in C#.
Example of using the Strategy Pattern in C#.
void Run()
Executes the example for the Strategy Pattern in C#.
Example of using the Visitor Pattern in C#.
void Run()
Executes the example for the Visitor Pattern in C#.
The namespace containing all Design Pattern Examples implemented in C#.
ExerciseList exercises
Definition: program.c:177
void(* Action)(void)
Alias for a function pointer, using C# as inspiration for the name.
Definition: program.c:39
Represents a single exercise or example for a design pattern.
Definition: Program.cs:27
Action exercise_to_run
Method to call to run the exercise.
Definition: Program.cs:36
string name
Name of the exercise.
Definition: Program.cs:31
Exercise(string nameOfExercise, Action exercise)
Constructor.
Definition: Program.cs:44
Represents the command line options provided to the program, if any.
Definition: Program.cs:55
List< string > exercise_names
List of exercise names to run. If this list is empty, run all exercises.
Definition: Program.cs:60