Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 874d250

Browse files
state pattern example
1 parent 65d64aa commit 874d250

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

‎DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisitorPattern", "VisitorPa
4343
EndProject
4444
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "StrategyPattern\StrategyPattern.csproj", "{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}"
4545
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePattern", "StatePattern\StatePattern.csproj", "{606D9229-082C-4A71-8DD3-1918A14B7CF2}"
47+
EndProject
4648
Global
4749
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4850
Debug|Any CPU = Debug|Any CPU
@@ -129,6 +131,10 @@ Global
129131
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Debug|Any CPU.Build.0 = Debug|Any CPU
130132
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Release|Any CPU.ActiveCfg = Release|Any CPU
131133
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Release|Any CPU.Build.0 = Release|Any CPU
134+
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135+
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
136+
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
137+
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Release|Any CPU.Build.0 = Release|Any CPU
132138
EndGlobalSection
133139
GlobalSection(SolutionProperties) = preSolution
134140
HideSolutionNode = FALSE

‎StatePattern/Program.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
3+
namespace StatePattern
4+
{
5+
interface IWritingState {
6+
7+
void Write(string words);
8+
9+
}
10+
11+
class UpperCase : IWritingState
12+
{
13+
public void Write(string words)
14+
{
15+
Console.WriteLine(words.ToUpper());
16+
}
17+
}
18+
19+
class LowerCase : IWritingState
20+
{
21+
public void Write(string words)
22+
{
23+
Console.WriteLine(words.ToLower());
24+
}
25+
}
26+
27+
class DefaultText : IWritingState
28+
{
29+
public void Write(string words)
30+
{
31+
Console.WriteLine(words);
32+
}
33+
}
34+
35+
class TextEditor {
36+
37+
private IWritingState mState;
38+
39+
public TextEditor()
40+
{
41+
mState = new DefaultText();
42+
}
43+
44+
public void SetState(IWritingState state)
45+
{
46+
mState = state;
47+
}
48+
49+
public void Type(string words)
50+
{
51+
mState.Write(words);
52+
}
53+
54+
}
55+
56+
class Program
57+
{
58+
static void Main(string[] args)
59+
{
60+
var editor = new TextEditor();
61+
62+
editor.Type("First line");
63+
64+
editor.SetState(new UpperCase());
65+
66+
editor.Type("Second Line");
67+
editor.Type("Third Line");
68+
69+
editor.SetState(new LowerCase());
70+
71+
editor.Type("Fourth Line");
72+
editor.Type("Fifthe Line");
73+
74+
// Output:
75+
// First line
76+
// SECOND LINE
77+
// THIRD LINE
78+
// fourth line
79+
// fifth line
80+
81+
Console.ReadLine();
82+
}
83+
}
84+
}

‎StatePattern/StatePattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /