From f9631ea99a4e76a696573570e5a39ae47b1e9b80 Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Thu, 30 Jun 2016 12:15:58 +0300 Subject: [PATCH] Add FanExample --- .../FanExample/FanMotivationalExample.cs | 75 ++++++++ .../FanExample/FanWithStatePatternExample.cs | 165 ++++++++++++++++++ src/StatePattern/StatePatternExamples.cs | 11 +- 3 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 src/StatePattern/FanExample/FanMotivationalExample.cs create mode 100644 src/StatePattern/FanExample/FanWithStatePatternExample.cs diff --git a/src/StatePattern/FanExample/FanMotivationalExample.cs b/src/StatePattern/FanExample/FanMotivationalExample.cs new file mode 100644 index 0000000..72dd9bd --- /dev/null +++ b/src/StatePattern/FanExample/FanMotivationalExample.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace StatePattern.FanExample +{ + public class FanMotivationalExample + { + public static void Run() + { + FanMotivationalContext fanContext = new FanMotivationalContext(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + } + } + + public class FanMotivationalContext + { + private const int OFF = 0; + + private const int S1 = 1; + + private const int S2 = 2; + + private const int S3 = 3; + + private const int S4 = 4; + + int state = OFF; + + public void PullChain() + { + if(state == OFF) + { + Console.WriteLine("Turning TV to speed 1"); + state = S1; + } + else if(state == S1) + { + Console.WriteLine("Turning TV to speed 2"); + state = S2; + } + else if (state == S2) + { + Console.WriteLine("Turning TV to speed 3"); + state = S3; + } + else if (state == S3) + { + Console.WriteLine("Turning TV to speed 4"); + state = S4; + } + else if (state == S4) + { + Console.WriteLine("Turning TV OFF"); + state = OFF; + } + } + + } +} diff --git a/src/StatePattern/FanExample/FanWithStatePatternExample.cs b/src/StatePattern/FanExample/FanWithStatePatternExample.cs new file mode 100644 index 0000000..69a1a22 --- /dev/null +++ b/src/StatePattern/FanExample/FanWithStatePatternExample.cs @@ -0,0 +1,165 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace StatePattern.FanExample +{ + public class FanWithStatePatternExample + { + public static void Run() + { + FanMotivationalContext fanContext = new FanMotivationalContext(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + + fanContext.PullChain(); + } + } + + /// + /// Context + /// + public class FanContext + { + public IFanState OFF { get; private set; } + + public IFanState S1 { get; private set; } + + public IFanState S2 { get; private set; } + + public IFanState S3 { get; private set; } + + public IFanState S4 { get; private set; } + + public IFanState State { get; set; } + + public FanContext() + { + OFF = new FanOffState(this); + + S1 = new FanS1State(this); + + S2 = new FanS2State(this); + + S3 = new FanS3State(this); + + S4 = new FanS4State(this); + + State = OFF; + } + + public void PullChain() + { + State.PullChain(); + } + } + + /// + /// State interface + /// + public interface IFanState + { + void PullChain(); + } + + public class FanOffState : IFanState + { + FanContext context; + public FanOffState(FanContext context) + { + this.context = context; + } + public void PullChain() + { + Console.WriteLine("Turning TV to speed 1"); + context.State = context.S1; + } + } + + /// + /// State concrete implementation + /// + public class FanS1State : IFanState + { + private FanContext fanContext; + + public FanS1State(FanContext fanContext) + { + this.fanContext = fanContext; + } + + public void PullChain() + { + Console.WriteLine("Turning TV to speed 2"); + fanContext.State = fanContext.S2; + } + } + + /// + /// State concrete implementation + /// + public class FanS2State : IFanState + { + private FanContext fanContext; + + public FanS2State(FanContext fanContext) + { + this.fanContext = fanContext; + } + + public void PullChain() + { + Console.WriteLine("Turning TV to speed 3"); + fanContext.State = fanContext.S3; + } + } + + /// + /// State concrete implementation + /// + public class FanS3State : IFanState + { + private FanContext fanContext; + + public FanS3State(FanContext fanContext) + { + this.fanContext = fanContext; + } + + public void PullChain() + { + Console.WriteLine("Turning TV to speed 4"); + fanContext.State = fanContext.S4; + } + } + + /// + /// State concrete implementation + /// + public class FanS4State : IFanState + { + private FanContext fanContext; + + public FanS4State(FanContext fanContext) + { + this.fanContext = fanContext; + } + + public void PullChain() + { + Console.WriteLine("Turning TV OFF"); + fanContext.State = fanContext.OFF; + } + } +} diff --git a/src/StatePattern/StatePatternExamples.cs b/src/StatePattern/StatePatternExamples.cs index ebee6fb..8ffa4a0 100644 --- a/src/StatePattern/StatePatternExamples.cs +++ b/src/StatePattern/StatePatternExamples.cs @@ -1,4 +1,5 @@ -using StatePattern.TVExample; +using StatePattern.FanExample; +using StatePattern.TVExample; using System; using System.Collections.Generic; using System.Linq; @@ -15,6 +16,14 @@ namespace StatePattern GoToNextStep(); TVExampleRunner.Run(); + + GoToNextStep(); + + FanMotivationalExample.Run(); + + GoToNextStep(); + + FanWithStatePatternExample.Run(); } private static void GoToNextStep()