十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先要理解 State Pattern 模式。
创新互联公司专业为企业提供烈山网站建设、烈山做网站、烈山网站设计、烈山网站制作等企业网站建设、网页设计与制作、烈山企业网站模板建站服务,10多年烈山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。http://www.dofactory.com/net/state-design-pattern
The classes and objects participating in this pattern are:
先画个状态机用来接收和处理数据。
开始定义Base状态和各个状态
public abstract class StateBase
{
public abstract void Enter(Monitor context);
public virtual void Exit(Monitor context)
{
Console.WriteLine("Exiting current state: {0}", context.CurrentState.StateName);
}
public string StateName
{
get;
set;
}
}
public class ConnectState : StateBase
{
public ConnectState()
{
this.StateName = "Connect";
}
public override void Enter(Monitor context)
{
Console.WriteLine("Enter - {0}", context.CurrentState.StateName);
context.MoveToNextState(new ReceiveDataState());
}
}
Create a context class, and set initial state to start running.
public class Monitor
{
public Monitor()
{
}
public void MoveToNextState(StateBase nextState)
{
Console.WriteLine("Changing state...");
this.CurrentState.Exit(this);
this.CurrentState = nextState;
this.CurrentState.Enter(this);
}
public void Start()
{
this.CurrentState = new NotStartState();
this.CurrentState.Enter(this);
}
public StateBase CurrentState
{
get;
set;
}
开始使用状态机
static void Main(string[] args)
{
Monitor m= new Monitor();
m.Start();
}