Add motivational example to TV example

This commit is contained in:
Petrutiu Mihai
2016-06-30 11:42:20 +03:00
parent 243a824e19
commit 9ad9211adc
7 changed files with 65 additions and 1 deletions

View File

@@ -10,7 +10,17 @@ namespace StatePattern
{
public static void Run()
{
TVMotivationalExample.Run();
GoToNextStep();
TVExampleRunner.Run();
}
private static void GoToNextStep()
{
Console.ReadKey();
Console.Clear();
}
}
}

View File

@@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace StatePattern
{
/// <summary>
/// State interface
/// </summary>
public interface ITVState
{
void OnPowerButtonPresed();

View File

@@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace StatePattern.TVExample
{
/// <summary>
/// Context
/// </summary>
public class TVContext : ITVState
{
public ITVState TvOnState { get; private set; }

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace StatePattern.TVExample
{
public class TVMotivationalExample
{
public static void Run()
{
var tvMotivationalContext = new TVMotivationalContext();
tvMotivationalContext.OnPowerButtonPresed();
tvMotivationalContext.OnPowerButtonPresed();
tvMotivationalContext.OnPowerButtonPresed();
tvMotivationalContext.OnPowerButtonPresed();
}
}
public class TVMotivationalContext
{
bool isTvOn = false;
public void OnPowerButtonPresed()
{
if(isTvOn)
{
Console.WriteLine("TV turning off");
isTvOn = false;
}
else
{
Console.WriteLine("Turning TV on");
isTvOn = true;
}
}
}
}

View File

@@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace StatePattern.TVExample
{
/// <summary>
/// Concrete state
/// </summary>
public class TVOffState : ITVState
{
TVContext context;

View File

@@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace StatePattern.TVExample
{
/// <summary>
/// Concrete state
/// </summary>
public class TVOnState : ITVState
{
TVContext context;