diff --git a/.gitignore b/.gitignore index 5e94794..4a333ea 100644 --- a/.gitignore +++ b/.gitignore @@ -245,3 +245,4 @@ ModelManifest.xml .fake/ /patterns-library /patterns-library.zip +/DeviceWithStateDesign/DeviceWithStateDesign diff --git a/BehavioralPatterns.sln b/BehavioralPatterns.sln index ddeddba..c6819c9 100644 --- a/BehavioralPatterns.sln +++ b/BehavioralPatterns.sln @@ -24,6 +24,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ObserverPattern", "src\Obse EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "BehavioralPatterns", "src\BehavioralPatterns\BehavioralPatterns.xproj", "{7843B0F8-7530-4859-8BEE-43D370E0F0AA}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePattern\StatePattern.xproj", "{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -70,5 +76,6 @@ Global {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {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} + {0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB} = {3820200F-354C-41E6-8F34-B301F5D621C2} EndGlobalSection EndGlobal diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index f86846b..da96247 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -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(); + } } } diff --git a/src/BehavioralPatterns/project.json b/src/BehavioralPatterns/project.json index 8444dae..523d334 100644 --- a/src/BehavioralPatterns/project.json +++ b/src/BehavioralPatterns/project.json @@ -14,7 +14,8 @@ "type": "platform", "version": "1.0.0" }, - "ObserverPattern": "1.0.0-*" + "ObserverPattern": "1.0.0-*", + "StatePattern": "1.0.0-*" }, "frameworks": { diff --git a/src/StatePattern/Properties/AssemblyInfo.cs b/src/StatePattern/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e4040aa --- /dev/null +++ b/src/StatePattern/Properties/AssemblyInfo.cs @@ -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")] diff --git a/src/StatePattern/StatePattern.xproj b/src/StatePattern/StatePattern.xproj new file mode 100644 index 0000000..f742fb7 --- /dev/null +++ b/src/StatePattern/StatePattern.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + 0b5b470d-45a4-4f6b-8dad-d0116c40b6fb + StatePattern + .\obj + .\bin\ + v4.6.1 + + + + 2.0 + + + diff --git a/src/StatePattern/StatePatternExamples.cs b/src/StatePattern/StatePatternExamples.cs new file mode 100644 index 0000000..90c7a56 --- /dev/null +++ b/src/StatePattern/StatePatternExamples.cs @@ -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(); + } + } +} diff --git a/src/StatePattern/TVExample/ITVState.cs b/src/StatePattern/TVExample/ITVState.cs new file mode 100644 index 0000000..d6496fe --- /dev/null +++ b/src/StatePattern/TVExample/ITVState.cs @@ -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(); + } +} diff --git a/src/StatePattern/TVExample/TVContext.cs b/src/StatePattern/TVExample/TVContext.cs new file mode 100644 index 0000000..c0f96c2 --- /dev/null +++ b/src/StatePattern/TVExample/TVContext.cs @@ -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(); + } + } +} diff --git a/src/StatePattern/TVExample/TVExample.cs b/src/StatePattern/TVExample/TVExample.cs new file mode 100644 index 0000000..ac73438 --- /dev/null +++ b/src/StatePattern/TVExample/TVExample.cs @@ -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(); + } + } +} diff --git a/src/StatePattern/TVExample/TVOffState.cs b/src/StatePattern/TVExample/TVOffState.cs new file mode 100644 index 0000000..ceccf65 --- /dev/null +++ b/src/StatePattern/TVExample/TVOffState.cs @@ -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"); + } + } +} diff --git a/src/StatePattern/TVExample/TVOnState.cs b/src/StatePattern/TVExample/TVOnState.cs new file mode 100644 index 0000000..0f0e560 --- /dev/null +++ b/src/StatePattern/TVExample/TVOnState.cs @@ -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"); + } + } +} diff --git a/src/StatePattern/project.json b/src/StatePattern/project.json new file mode 100644 index 0000000..864b9a5 --- /dev/null +++ b/src/StatePattern/project.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.6.0" + }, + + "frameworks": { + "netstandard1.6": { + "imports": "dnxcore50" + } + } +}