Skip to content

Commit f70599a

Browse files
committed
state pattern example
1 parent 7993d59 commit f70599a

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

README.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,72 +2136,74 @@ Let's take an example of text editor, it lets you change the state of text that
21362136
First of all we have our state interface and some state implementations
21372137

21382138
```C#
2139-
interface WritingState
2140-
{
2141-
public function write(string $words);
2139+
interface IWritingState {
2140+
2141+
void Write(string words);
2142+
21422143
}
21432144

2144-
class UpperCase implements WritingState
2145+
class UpperCase : IWritingState
21452146
{
2146-
public function write(string $words)
2147-
{
2148-
echo strtoupper($words);
2149-
}
2147+
public void Write(string words)
2148+
{
2149+
Console.WriteLine(words.ToUpper());
2150+
}
21502151
}
21512152

2152-
class LowerCase implements WritingState
2153+
class LowerCase : IWritingState
21532154
{
2154-
public function write(string $words)
2155-
{
2156-
echo strtolower($words);
2157-
}
2155+
public void Write(string words)
2156+
{
2157+
Console.WriteLine(words.ToLower());
2158+
}
21582159
}
21592160

2160-
class DefaultText implements WritingState
2161+
class DefaultText : IWritingState
21612162
{
2162-
public function write(string $words)
2163-
{
2164-
echo $words;
2165-
}
2163+
public void Write(string words)
2164+
{
2165+
Console.WriteLine(words);
2166+
}
21662167
}
21672168
```
21682169
Then we have our editor
21692170
```C#
2170-
class TextEditor
2171-
{
2172-
protected $state;
2171+
class TextEditor {
21732172

2174-
public function __construct(WritingState $state)
2175-
{
2176-
$this->state = $state;
2177-
}
2173+
private IWritingState mState;
21782174

2179-
public function setState(WritingState $state)
2180-
{
2181-
$this->state = $state;
2182-
}
2175+
public TextEditor()
2176+
{
2177+
mState = new DefaultText();
2178+
}
2179+
2180+
public void SetState(IWritingState state)
2181+
{
2182+
mState = state;
2183+
}
2184+
2185+
public void Type(string words)
2186+
{
2187+
mState.Write(words);
2188+
}
21832189

2184-
public function type(string $words)
2185-
{
2186-
$this->state->write($words);
2187-
}
21882190
}
21892191
```
21902192
And then it can be used as
21912193
```C#
2192-
$editor = new TextEditor(new DefaultText());
2194+
var editor = new TextEditor();
21932195

2194-
$editor->type('First line');
2196+
editor.Type("First line");
21952197

2196-
$editor->setState(new UpperCase());
2198+
editor.SetState(new UpperCase());
21972199

2198-
$editor->type('Second line');
2199-
$editor->type('Third line');
2200+
editor.Type("Second Line");
2201+
editor.Type("Third Line");
22002202

2201-
$editor->setState(new LowerCase());
2203+
editor.SetState(new LowerCase());
22022204

2203-
$editor->type('Fourth line');
2204-
$editor->type('Fifth line');
2205+
editor.Type("Fourth Line");
2206+
editor.Type("Fifthe Line");
22052207

22062208
// Output:
22072209
// First line

0 commit comments

Comments
 (0)