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