State_Model
抽象狀態State
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
abstract class State
{
public abstract void WriteProgram(Work work);
}
}
工作類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class Work
{
private State current;
public Work()
{
current = new ForenoonState();
}
private double hour;
public double Hour
{
get { return hour; }
set { hour = value; }
}
private bool finish = false;
public bool TaskFinished
{
get { return finish; }
set { finish = value; }
}
public void SetState( State s)
{
current = s;
}
public void WriteProgram()
{
current.WriteProgram(this);
}
}
}
早上狀態類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class ForenoonState : State
{
public override void WriteProgram(Work work)
{
if (work.Hour < 12)
{
Console.WriteLine("當前時間 {0}點 上午工作,精神百倍",work.Hour);
}
else
{
work.SetState(new NoonState());
work.WriteProgram();
}
}
}
}
中午狀態類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class NoonState:State
{
public override void WriteProgram(Work work)
{
if (work.Hour < 13)
{
Console.WriteLine("當前時間:{0}點 餓了,午飯:犯困,午休!",work.Hour);
}
else
{
work.SetState(new AfternoonState());
work.WriteProgram();
}
}
}
}
下午類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class AfternoonState : State
{
public override void WriteProgram(Work work)
{
if (work.Hour < 17 )
{
Console.WriteLine("當前時間:{0}點 餓了,下午狀態還不錯,繼續努力!", work.Hour);
}
else
{
work.SetState(new EveningState());
work.WriteProgram();
}
}
}
}
晚上狀態類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class AfternoonState : State
{
public override void WriteProgram(Work work)
{
if (work.Hour < 17 )
{
Console.WriteLine("當前時間:{0}點 餓了,下午狀態還不錯,繼續努力!", work.Hour);
}
else
{
work.SetState(new EveningState());
work.WriteProgram();
}
}
}
}
休息狀態類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class RestState : State
{
public override void WriteProgram(Work work)
{
Console.WriteLine("當前時間 :{0}點,下班回家了",work.Hour);
}
}
}
睡覺狀態類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class SleepingState : State
{
public override void WriteProgram(Work work)
{
Console.WriteLine("當前時間: {0}點,睡着了",work.Hour);
}
}
}
客戶端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 工作狀態
{
class Program
{
static void Main(string[] args)
{
Work workOne = new Work();
workOne.Hour = 9;
workOne.WriteProgram();
workOne.Hour = 10;
workOne.WriteProgram();
workOne.Hour = 12;
workOne.WriteProgram();
workOne.Hour = 13;
workOne.WriteProgram();
workOne.Hour = 14;
workOne.WriteProgram();
workOne.Hour = 17;
workOne.WriteProgram();
workOne.Hour = 19;
workOne.WriteProgram();
workOne.Hour = 21;
workOne.WriteProgram();
Console.ReadKey();
}
}
}
總結:狀態模式通過把各種狀態轉移邏輯分佈到State的子類之間,來減少相互間的依賴。當一個對象的行爲取決於它的狀態的時候,並且它必須在運行時根據狀態改變它的行爲時就可以考慮使用狀態模式了。