Add state pattern example
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
"type": "platform",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"ObserverPattern": "1.0.0-*"
|
||||
"ObserverPattern": "1.0.0-*",
|
||||
"StatePattern": "1.0.0-*"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
19
src/StatePattern/Properties/AssemblyInfo.cs
Normal file
19
src/StatePattern/Properties/AssemblyInfo.cs
Normal 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")]
|
||||
21
src/StatePattern/StatePattern.xproj
Normal file
21
src/StatePattern/StatePattern.xproj
Normal 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>
|
||||
16
src/StatePattern/StatePatternExamples.cs
Normal file
16
src/StatePattern/StatePatternExamples.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/StatePattern/TVExample/ITVState.cs
Normal file
13
src/StatePattern/TVExample/ITVState.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
22
src/StatePattern/TVExample/TVContext.cs
Normal file
22
src/StatePattern/TVExample/TVContext.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/StatePattern/TVExample/TVExample.cs
Normal file
25
src/StatePattern/TVExample/TVExample.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/StatePattern/TVExample/TVOffState.cs
Normal file
16
src/StatePattern/TVExample/TVOffState.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/StatePattern/TVExample/TVOnState.cs
Normal file
16
src/StatePattern/TVExample/TVOnState.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/StatePattern/project.json
Normal file
13
src/StatePattern/project.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"netstandard1.6": {
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user