Update TV example
This commit is contained in:
@@ -8,11 +8,16 @@ namespace StatePattern.TVExample
|
|||||||
{
|
{
|
||||||
public class TVContext : ITVState
|
public class TVContext : ITVState
|
||||||
{
|
{
|
||||||
public TVContext(ITVState initialState)
|
public ITVState TvOnState { get; private set; }
|
||||||
{
|
public ITVState TvOffState { get; private set; }
|
||||||
State = initialState;
|
|
||||||
}
|
|
||||||
public ITVState State { get; set; }
|
public ITVState State { get; set; }
|
||||||
|
public TVContext()
|
||||||
|
{
|
||||||
|
TvOnState = new TVOnState(this);
|
||||||
|
TvOffState = new TVOffState(this);
|
||||||
|
State = TvOffState;
|
||||||
|
}
|
||||||
|
|
||||||
public void OnPowerButtonPresed()
|
public void OnPowerButtonPresed()
|
||||||
{
|
{
|
||||||
State.OnPowerButtonPresed();
|
State.OnPowerButtonPresed();
|
||||||
|
|||||||
@@ -10,14 +10,13 @@ namespace StatePattern.TVExample
|
|||||||
{
|
{
|
||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
ITVState tvOnState = new TVOnState();
|
TVContext context = new TVContext();
|
||||||
ITVState tvOffState = new TVOffState();
|
|
||||||
|
|
||||||
TVContext context = new TVContext(tvOffState);
|
|
||||||
|
|
||||||
context.OnPowerButtonPresed();
|
context.OnPowerButtonPresed();
|
||||||
|
|
||||||
context.State = tvOnState;
|
context.OnPowerButtonPresed();
|
||||||
|
|
||||||
|
context.OnPowerButtonPresed();
|
||||||
|
|
||||||
context.OnPowerButtonPresed();
|
context.OnPowerButtonPresed();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,16 @@ namespace StatePattern.TVExample
|
|||||||
{
|
{
|
||||||
public class TVOffState : ITVState
|
public class TVOffState : ITVState
|
||||||
{
|
{
|
||||||
|
TVContext context;
|
||||||
|
public TVOffState(TVContext context)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
public void OnPowerButtonPresed()
|
public void OnPowerButtonPresed()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Turning TV on");
|
Console.WriteLine("Turning TV on");
|
||||||
|
context.State = context.TvOnState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,16 @@ namespace StatePattern.TVExample
|
|||||||
{
|
{
|
||||||
public class TVOnState : ITVState
|
public class TVOnState : ITVState
|
||||||
{
|
{
|
||||||
|
TVContext context;
|
||||||
|
public TVOnState(TVContext context)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
public void OnPowerButtonPresed()
|
public void OnPowerButtonPresed()
|
||||||
{
|
{
|
||||||
Console.WriteLine("TV turning off");
|
Console.WriteLine("TV turning off");
|
||||||
|
context.State = context.TvOffState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user