StatePattern
class Program { static void Main(string[] args) { var state = new OpeningState(); var lift = new Lift(); lift.SetListState(state); lift.Close(); lift.Open(); lift.Run(); lift.Stop(); Console.Read(); } } public abstract class LiftState { protected Lift Lift; public void SetLift(Lift lift) { this.Lift = lift; } public abstract void Open(); public abstract void Close(); public abstract void Run(); public abstract void Stop(); } public class OpeningState : LiftState { public override void Open() { Console.WriteLine("电梯打开!"); } public override void Close() { base.Lift.SetListState(Lift.CloseingState); base.Lift.Close(); } public override void Run() { Console.WriteLine("打开不能运行"); } public override void Stop() { base.Lift.SetListState(Lift.StopingState); base.Lift.Stop(); } } public class ClosingState : LiftState { public override void Open() { base.Lift.SetListState(Lift.OpeningState); base.Lift.Open(); } public override void Close() { Console.WriteLine("电梯关闭!"); } public override void Run() { base.Lift.SetListState(Lift.RuningState); base.Lift.Open(); } public override void Stop() { base.Lift.SetListState(Lift.StopingState); base.Lift.Stop(); } } public class RuningState : LiftState { public override void Open() { base.Lift.SetListState(Lift.OpeningState); base.Lift.Open(); } public override void Close() { base.Lift.SetListState(Lift.CloseingState); base.Lift.Close(); } public override void Run() { Console.WriteLine("电梯运行!"); } public override void Stop() { base.Lift.SetListState(Lift.StopingState); base.Lift.Stop(); } } public class StopingState : LiftState { public override void Open() { base.Lift.SetListState(Lift.OpeningState); base.Lift.Open(); } public override void Close() { base.Lift.SetListState(Lift.CloseingState); base.Lift.Close(); } public override void Run() { base.Lift.SetListState(Lift.RuningState); base.Lift.Open(); } public override void Stop() { Console.WriteLine("电梯停止!"); } } public class Lift { public static LiftState OpeningState = new OpeningState(); public static LiftState CloseingState = new ClosingState(); public static LiftState RuningState = new RuningState(); public static LiftState StopingState = new StopingState(); private LiftState _liftState; public LiftState GetState() { return this._liftState; } public void SetListState(LiftState state) { this._liftState = state; this._liftState.SetLift(this); } public void Open() { this._liftState.Open(); } public void Close() { this._liftState.Close(); } public void Stop() { this._liftState.Stop(); } public void Run() { this._liftState.Run(); } }