Add motivational example to TV example
This commit is contained in:
@@ -10,7 +10,17 @@ namespace StatePattern
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
TVMotivationalExample.Run();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
TVExampleRunner.Run();
|
||||
}
|
||||
|
||||
private static void GoToNextStep()
|
||||
{
|
||||
Console.ReadKey();
|
||||
Console.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StatePattern
|
||||
{
|
||||
/// <summary>
|
||||
/// State interface
|
||||
/// </summary>
|
||||
public interface ITVState
|
||||
{
|
||||
void OnPowerButtonPresed();
|
||||
|
||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StatePattern.TVExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Context
|
||||
/// </summary>
|
||||
public class TVContext : ITVState
|
||||
{
|
||||
public ITVState TvOnState { get; private set; }
|
||||
|
||||
42
src/StatePattern/TVExample/TVMotivationalExample.cs
Normal file
42
src/StatePattern/TVExample/TVMotivationalExample.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StatePattern.TVExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Concrete state
|
||||
/// </summary>
|
||||
public class TVOffState : ITVState
|
||||
{
|
||||
TVContext context;
|
||||
|
||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StatePattern.TVExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Concrete state
|
||||
/// </summary>
|
||||
public class TVOnState : ITVState
|
||||
{
|
||||
TVContext context;
|
||||
|
||||
Reference in New Issue
Block a user