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

View File

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

View File

@@ -14,7 +14,8 @@
"type": "platform",
"version": "1.0.0"
},
"ObserverPattern": "1.0.0-*"
"ObserverPattern": "1.0.0-*",
"StatePattern": "1.0.0-*"
},
"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"
}
}
}