首页 > 代码库 > 状态模式
状态模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
*/
namespace App_MYCS.HDL_SJMS.ZTMS
{
class my_ZTMS
{
public void dy()
{
//创业并初始化状态
Context c1 = new Context(new concreteStateA());
c1.Request();//转换下一个状态
c1.Request();//接着转换状态
}
}
//定义抽像状态变化处理
abstract class State
{
public abstract void Handle(Context context);
}
//定义状态类
class Context
{
private State state;
public Context(State state)
{
this.state = state;
}
public State State
{
get { return state; }
set {
state = value;
Console.WriteLine("当前状态");
}
}
public void Request()
{
state.Handle(this);
}
}
//子类状态转换
class concreteStateA : State
{
public override void Handle(Context context)
{
context.State = new concreteStateB();
}
}
class concreteStateB : State
{
public override void Handle(Context context)
{
context.State = new concreteStateA();
}
}
}
状态模式