Add FanExample
This commit is contained in:
75
src/StatePattern/FanExample/FanMotivationalExample.cs
Normal file
75
src/StatePattern/FanExample/FanMotivationalExample.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
165
src/StatePattern/FanExample/FanWithStatePatternExample.cs
Normal file
165
src/StatePattern/FanExample/FanWithStatePatternExample.cs
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Context
|
||||||
|
/// </summary>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State interface
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State concrete implementation
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State concrete implementation
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State concrete implementation
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State concrete implementation
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using StatePattern.TVExample;
|
using StatePattern.FanExample;
|
||||||
|
using StatePattern.TVExample;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -15,6 +16,14 @@ namespace StatePattern
|
|||||||
GoToNextStep();
|
GoToNextStep();
|
||||||
|
|
||||||
TVExampleRunner.Run();
|
TVExampleRunner.Run();
|
||||||
|
|
||||||
|
GoToNextStep();
|
||||||
|
|
||||||
|
FanMotivationalExample.Run();
|
||||||
|
|
||||||
|
GoToNextStep();
|
||||||
|
|
||||||
|
FanWithStatePatternExample.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GoToNextStep()
|
private static void GoToNextStep()
|
||||||
|
|||||||
Reference in New Issue
Block a user