Add motivational example to TV example
This commit is contained in:
@@ -71,7 +71,6 @@ Concrete Observer -> Implementation of the observer
|
|||||||
";
|
";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static string GetLapsedLinstenerProblem()
|
private static string GetLapsedLinstenerProblem()
|
||||||
{
|
{
|
||||||
return @"
|
return @"
|
||||||
@@ -80,6 +79,7 @@ Consequently, the publisher still holds a reference to the observer which preven
|
|||||||
— including all other objects it is referring to — for as long as the publisher is alive, which could be until the end of the application.
|
— including all other objects it is referring to — for as long as the publisher is alive, which could be until the end of the application.
|
||||||
This causes not only a memory leak, but also a performance degradation with an 'uninterested' observer receiving and acting on unwanted events";
|
This causes not only a memory leak, but also a performance degradation with an 'uninterested' observer receiving and acting on unwanted events";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GoToNextStep()
|
private static void GoToNextStep()
|
||||||
{
|
{
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|||||||
@@ -10,7 +10,17 @@ namespace StatePattern
|
|||||||
{
|
{
|
||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
|
TVMotivationalExample.Run();
|
||||||
|
|
||||||
|
GoToNextStep();
|
||||||
|
|
||||||
TVExampleRunner.Run();
|
TVExampleRunner.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void GoToNextStep()
|
||||||
|
{
|
||||||
|
Console.ReadKey();
|
||||||
|
Console.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace StatePattern
|
namespace StatePattern
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// State interface
|
||||||
|
/// </summary>
|
||||||
public interface ITVState
|
public interface ITVState
|
||||||
{
|
{
|
||||||
void OnPowerButtonPresed();
|
void OnPowerButtonPresed();
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace StatePattern.TVExample
|
namespace StatePattern.TVExample
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Context
|
||||||
|
/// </summary>
|
||||||
public class TVContext : ITVState
|
public class TVContext : ITVState
|
||||||
{
|
{
|
||||||
public ITVState TvOnState { get; private set; }
|
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
|
namespace StatePattern.TVExample
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Concrete state
|
||||||
|
/// </summary>
|
||||||
public class TVOffState : ITVState
|
public class TVOffState : ITVState
|
||||||
{
|
{
|
||||||
TVContext context;
|
TVContext context;
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace StatePattern.TVExample
|
namespace StatePattern.TVExample
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Concrete state
|
||||||
|
/// </summary>
|
||||||
public class TVOnState : ITVState
|
public class TVOnState : ITVState
|
||||||
{
|
{
|
||||||
TVContext context;
|
TVContext context;
|
||||||
|
|||||||
Reference in New Issue
Block a user