Skip to content

Commit 9f43976

Browse files
committed
Add State pattern.
1 parent 429f79b commit 9f43976

File tree

8 files changed

+183
-0
lines changed

8 files changed

+183
-0
lines changed

Behavioral/State/Client.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace State {
4+
5+
class Client {
6+
7+
static void Main(string[] args) {
8+
var sc = new StateContext();
9+
sc.WriteName("Monday");
10+
sc.WriteName("Tuesday");
11+
sc.WriteName("Wednesday");
12+
sc.WriteName("Thursday");
13+
sc.WriteName("Saturday");
14+
sc.WriteName("Sunday");
15+
16+
Console.ReadKey();
17+
}
18+
}
19+
}

Behavioral/State/IState.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace State {
2+
3+
public interface IState {
4+
5+
void WriteName(StateContext stateContext, string name);
6+
7+
}
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("State")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("State")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("61a69783-73c6-4634-9c1e-97dc1992b3b0")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Behavioral/State/State.csproj

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{F214B483-E8BB-477D-807F-24F176B588D5}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>State</RootNamespace>
12+
<AssemblyName>State</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<PropertyGroup>
37+
<StartupObject>State.Client</StartupObject>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="IState.cs" />
50+
<Compile Include="Client.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
<Compile Include="StateA.cs" />
53+
<Compile Include="StateB.cs" />
54+
<Compile Include="StateContext.cs" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
58+
Other similar extension points exist, see Microsoft.Common.targets.
59+
<Target Name="BeforeBuild">
60+
</Target>
61+
<Target Name="AfterBuild">
62+
</Target>
63+
-->
64+
</Project>

Behavioral/State/StateA.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace State {
4+
5+
public class StateA : IState {
6+
7+
public void WriteName(StateContext stateContext, string name) {
8+
Console.WriteLine(name.ToLower());
9+
stateContext.SetState(new StateB());
10+
}
11+
}
12+
}

Behavioral/State/StateB.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace State {
4+
5+
public class StateB : IState {
6+
7+
private int _count;
8+
9+
public void WriteName(StateContext stateContext, string name) {
10+
Console.WriteLine(name.ToUpper());
11+
if (++this._count > 1) {
12+
stateContext.SetState(new StateA());
13+
}
14+
}
15+
}
16+
}

Behavioral/State/StateContext.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace State {
2+
3+
public class StateContext {
4+
5+
private IState _state;
6+
7+
public StateContext() {
8+
this._state = new StateA();
9+
}
10+
11+
public void SetState(IState state) {
12+
this._state = state;
13+
}
14+
15+
public void WriteName(string name) {
16+
this._state.WriteName(this, name);
17+
}
18+
}
19+
}

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memento", "Behavioral\Memen
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Observer", "Behavioral\Observer\Observer.csproj", "{2823F21D-D474-447D-9C8B-74B29D0D9B35}"
1919
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "State", "Behavioral\State\State.csproj", "{F214B483-E8BB-477D-807F-24F176B588D5}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,12 @@ Global
6971
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|Any CPU.ActiveCfg = Release|x86
7072
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|x86.ActiveCfg = Release|x86
7173
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|x86.Build.0 = Release|x86
74+
{F214B483-E8BB-477D-807F-24F176B588D5}.Debug|Any CPU.ActiveCfg = Debug|x86
75+
{F214B483-E8BB-477D-807F-24F176B588D5}.Debug|x86.ActiveCfg = Debug|x86
76+
{F214B483-E8BB-477D-807F-24F176B588D5}.Debug|x86.Build.0 = Debug|x86
77+
{F214B483-E8BB-477D-807F-24F176B588D5}.Release|Any CPU.ActiveCfg = Release|x86
78+
{F214B483-E8BB-477D-807F-24F176B588D5}.Release|x86.ActiveCfg = Release|x86
79+
{F214B483-E8BB-477D-807F-24F176B588D5}.Release|x86.Build.0 = Release|x86
7280
EndGlobalSection
7381
GlobalSection(SolutionProperties) = preSolution
7482
HideSolutionNode = FALSE
@@ -81,5 +89,6 @@ Global
8189
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
8290
{C404876C-B04B-451F-B159-2C0CF8CB861B} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
8391
{2823F21D-D474-447D-9C8B-74B29D0D9B35} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
92+
{F214B483-E8BB-477D-807F-24F176B588D5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
8493
EndGlobalSection
8594
EndGlobal

0 commit comments

Comments
 (0)