Design Pattern Examples
Overview of object-oriented design patterns
State_Exercise.cs
Go to the documentation of this file.
1
5
6
using
System;
7
8
namespace
DesignPatternExamples_csharp
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:"
);
69
_State_DisplayText
(
textToFilter
);
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
}
textToFilter
static const char * textToFilter
Definition:
State_Exercise.c:52
DesignPatternExamples_csharp.State_Exercise
Example of using the State Pattern in C#.
Definition:
State_Exercise.cs:24
DesignPatternExamples_csharp.State_Exercise.Run
void Run()
Executes the example for the State Pattern in C#.
Definition:
State_Exercise.cs:46
DesignPatternExamples_csharp.State_Exercise._State_DisplayText
void _State_DisplayText(string textToDisplay)
Helper method to display text from the State exercise. Text is displayed with line numbers.
Definition:
State_Exercise.cs:31
DesignPatternExamples_csharp.StateContext_Class
Represents the state machine. This maintains the context in which the state machine runs.
Definition:
State_Class.cs:696
DesignPatternExamples_csharp.StateContext_Class.RemoveComments
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
DesignPatternExamples_csharp
The namespace containing all Design Pattern Examples implemented in C#.
Definition:
Adapter_BackEndFunctions.cs:19
csharp
State_Exercise.cs
Generated on Tue Aug 29 2023 19:47:44 for Design Pattern Examples by
1.9.6