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

@@ -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();

View File

@@ -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();
}
} }
} }

View File

@@ -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();

View File

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

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 namespace StatePattern.TVExample
{ {
/// <summary>
/// Concrete state
/// </summary>
public class TVOffState : ITVState public class TVOffState : ITVState
{ {
TVContext context; TVContext context;

View File

@@ -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;