Add FanExample

This commit is contained in:
Petrutiu Mihai
2016-06-30 12:15:58 +03:00
parent 9ad9211adc
commit f9631ea99a
3 changed files with 250 additions and 1 deletions

View 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;
}
}
}
}