Design Pattern Examples
Overview of object-oriented design patterns
State_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7
9{
23 internal class State_Exercise
24 {
25
31 void _State_DisplayText(string textToDisplay)
32 {
33 string[] lines = textToDisplay.Split('\n');
34 int lineNumber = 1;
35 foreach (string line in lines)
36 {
37 Console.WriteLine(" {0,2}) {1}", lineNumber, line);
38 ++lineNumber;
39 }
40 }
41
45 // ! [Using State in C#]
46 public void Run()
47 {
48 Console.WriteLine();
49 Console.WriteLine("State Exercise");
50
51 StateContext_Class filterContext = new StateContext_Class();
52 string textToFilter =
53 "/*#################### Block Comment #################################*/\n" +
54 "//#################### Line Comment ####################################\n" +
55 "// A comment. /* A nested comment */\n" +
56 "\n" +
57 "void State_Exercise() // An exercise in state machines\n" +
58 "{\n" +
59 " char character = '\\\"';\n" +
60 " Console.WriteLine();\n" +
61 " Console.WriteLine(\"\\\"State\\\" /*Exercise*/\");\n" +
62 "\n" +
63 " StateContext_Class filterContext = new StateContext_Class();\n" +
64 "\n" +
65 " Console.WriteLine(\"\\t\\tDone. //(No, really)//\");\n" +
66 "}";
67
68 Console.WriteLine(" Text to filter:");
70
71 Console.WriteLine(" Filtering text...");
72 string filteredText = filterContext.RemoveComments(textToFilter);
73
74 Console.WriteLine(" Filtered text:");
75 _State_DisplayText(filteredText);
76
77 Console.WriteLine(" Done.");
78 }
79 // ! [Using State in C#]
80 }
81}
static const char * textToFilter
Example of using the State Pattern in C#.
void Run()
Executes the example for the State Pattern in C#.
void _State_DisplayText(string textToDisplay)
Helper method to display text from the State exercise. Text is displayed with line numbers.
Represents the state machine. This maintains the context in which the state machine runs.
Definition: State_Class.cs:696
string RemoveComments(string text)
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
Definition: State_Class.cs:769
The namespace containing all Design Pattern Examples implemented in C#.