Add state pattern example

This commit is contained in:
Petrutiu Mihai
2016-06-30 10:34:56 +03:00
parent 8d7babb645
commit 10175aca16
13 changed files with 189 additions and 11 deletions

1
.gitignore vendored
View File

@@ -245,3 +245,4 @@ ModelManifest.xml
.fake/ .fake/
/patterns-library /patterns-library
/patterns-library.zip /patterns-library.zip
/DeviceWithStateDesign/DeviceWithStateDesign

View File

@@ -24,6 +24,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ObserverPattern", "src\Obse
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "BehavioralPatterns", "src\BehavioralPatterns\BehavioralPatterns.xproj", "{7843B0F8-7530-4859-8BEE-43D370E0F0AA}" Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "BehavioralPatterns", "src\BehavioralPatterns\BehavioralPatterns.xproj", "{7843B0F8-7530-4859-8BEE-43D370E0F0AA}"
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePattern\StatePattern.xproj", "{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -58,6 +60,10 @@ Global
{7843B0F8-7530-4859-8BEE-43D370E0F0AA}.Debug|Any CPU.Build.0 = Debug|Any CPU {7843B0F8-7530-4859-8BEE-43D370E0F0AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7843B0F8-7530-4859-8BEE-43D370E0F0AA}.Release|Any CPU.ActiveCfg = Release|Any CPU {7843B0F8-7530-4859-8BEE-43D370E0F0AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7843B0F8-7530-4859-8BEE-43D370E0F0AA}.Release|Any CPU.Build.0 = Release|Any CPU {7843B0F8-7530-4859-8BEE-43D370E0F0AA}.Release|Any CPU.Build.0 = Release|Any CPU
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -70,5 +76,6 @@ Global
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {3820200F-354C-41E6-8F34-B301F5D621C2} {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2} {D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{7843B0F8-7530-4859-8BEE-43D370E0F0AA} = {3820200F-354C-41E6-8F34-B301F5D621C2} {7843B0F8-7530-4859-8BEE-43D370E0F0AA} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB} = {3820200F-354C-41E6-8F34-B301F5D621C2}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@@ -4,6 +4,7 @@ using IteratorPattern;
using MediatorPattern; using MediatorPattern;
using MememntoPattern; using MememntoPattern;
using ObserverPattern; using ObserverPattern;
using StatePattern;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -15,34 +16,41 @@ namespace BehavioralPatterns
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
ObserverPatternExamples.Run(); StatePatternExamples.Run();
Console.ReadKey(); GoToNextStep();
//Chain of responsibillity //Chain of responsibillity
//This is usefull when you have a request and you don't know who should process it //This is usefull when you have a request and you don't know who should process it
ChainOfResponsibillityExamples.Run(); ChainOfResponsibillityExamples.Run();
Console.ReadKey(); GoToNextStep();
Console.Clear();
CommandPatternExamples.Run(); CommandPatternExamples.Run();
Console.ReadKey(); GoToNextStep();
Console.Clear();
IteratorPatternExamples.Run(); IteratorPatternExamples.Run();
Console.ReadKey(); GoToNextStep();
IteratorPatternExamples.Run(); IteratorPatternExamples.Run();
Console.ReadKey(); GoToNextStep();
MediatorPatternExamples.Run(); MediatorPatternExamples.Run();
Console.ReadKey(); GoToNextStep();
MementoPatternExamples.Run(); MementoPatternExamples.Run();
GoToNextStep();
ObserverPatternExamples.Run();
GoToNextStep();
}
private static void GoToNextStep()
{
Console.ReadKey(); Console.ReadKey();
Console.Clear();
} }
} }
} }

View File

@@ -14,7 +14,8 @@
"type": "platform", "type": "platform",
"version": "1.0.0" "version": "1.0.0"
}, },
"ObserverPattern": "1.0.0-*" "ObserverPattern": "1.0.0-*",
"StatePattern": "1.0.0-*"
}, },
"frameworks": { "frameworks": {

View File

@@ -0,0 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Hewlett-Packard Company")]
[assembly: AssemblyProduct("StatePattern")]
[assembly: AssemblyTrademark("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0b5b470d-45a4-4f6b-8dad-d0116c40b6fb")]

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>0b5b470d-45a4-4f6b-8dad-d0116c40b6fb</ProjectGuid>
<RootNamespace>StatePattern</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@@ -0,0 +1,16 @@
using StatePattern.TVExample;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace StatePattern
{
public class StatePatternExamples
{
public static void Run()
{
TVExampleRunner.Run();
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatePattern
{
public interface ITVState
{
void OnPowerButtonPresed();
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatePattern.TVExample
{
public class TVContext : ITVState
{
ITVState initialState;
public TVContext(ITVState initialState)
{
State = initialState;
}
public ITVState State { get; set; }
public void OnPowerButtonPresed()
{
State.OnPowerButtonPresed();
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatePattern.TVExample
{
public class TVExampleRunner
{
public static void Run()
{
ITVState tvOnState = new TVOnState();
ITVState tvOffState = new TVOffState();
TVContext context = new TVContext(tvOffState);
context.OnPowerButtonPresed();
context.State = tvOnState;
context.OnPowerButtonPresed();
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatePattern.TVExample
{
public class TVOffState : ITVState
{
public void OnPowerButtonPresed()
{
Console.WriteLine("Turning TV on");
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatePattern.TVExample
{
public class TVOnState : ITVState
{
public void OnPowerButtonPresed()
{
Console.WriteLine("TV turning off");
}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}