状态机 - State Machine! C#.Net模拟电梯控制系统!
状态机 - State Machine! C#.Net模拟电梯控制系统!
状态机接口
电梯测试界面
电梯运行时状态
public class StateRunning : IElevatorState
{
private ElevatorController _ec;
public StateRunning(ElevatorController ec)
{
_ec = ec;
}
public void OpenDoor()
{
_ec.WriteLog("StateRunning.OpenDoor():[方法无效]不要命啦!电梯在升降中,不能开门!!!");
}
public void CloseDoor()
{
_ec.WriteLog("StateRunning.CloseDoor():[方法无效]电梯在运行,门已经是状态状态!!!");
}
public void Run()
{
_ec.WriteLog("StateRunning.Run():[方法无效]电梯已经在运行状态!!!");
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加微信
请下载附件!(Source for VIP)
状态机(State Machine)
状态机,一个十分难以理解的设计模式,是个超级逻辑思维的设计模式,在某些应用场景若不运用状态机真是不可想象,比如电梯控制系统,除了使用状态机没有其它更适合的设计模式!
当然,你可以写至少10000个 if 来判断电梯运行的各个状态,也可以实现滴!哈哈~ 若这样写的电梯系统是无法理解的不可维护的!
作者使用状态机模拟电梯系统
状态机接口
/// <summary>
/// 表示状态的接口
/// </summary>
public interface IElevatorState
{
void OpenDoor(); //开电梯门
void CloseDoor(); //关门
void Run(); //电梯运行(升/降)
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// 表示状态的接口
/// </summary>
public interface IElevatorState
{
void OpenDoor(); //开电梯门
void CloseDoor(); //关门
void Run(); //电梯运行(升/降)
}
// 来源:www.CSFramework.com, C/S结构框架学习网
电梯测试界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StateMachine
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
ElevatorController ec = new ElevatorController(this.listBox1);
ec.WriteLog("...测试打开电梯门...");
ec.SetCurrentState(ec.StateRunning);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorOpening);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorOpened);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorClosing);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorClosed);
ec.OpenDoor();//测试打开电梯门
}
private void button2_Click(object sender, EventArgs e)
{
ElevatorController ec = new ElevatorController(this.listBox1);
ec.WriteLog("...测试关闭电梯门...");
ec.SetCurrentState(ec.StateRunning);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorOpening);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorOpened);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorClosing);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorClosed);
ec.CloseDoor();//测试关闭电梯门
}
private void button3_Click(object sender, EventArgs e)
{
ElevatorController ec = new ElevatorController(this.listBox1);
ec.WriteLog("...测试运行电梯...");
ec.SetCurrentState(ec.StateRunning);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorOpening);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorOpened);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorClosing);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorClosed);
ec.Run();//测试运行电梯
}
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StateMachine
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
ElevatorController ec = new ElevatorController(this.listBox1);
ec.WriteLog("...测试打开电梯门...");
ec.SetCurrentState(ec.StateRunning);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorOpening);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorOpened);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorClosing);
ec.OpenDoor();//测试打开电梯门
ec.SetCurrentState(ec.StateDoorClosed);
ec.OpenDoor();//测试打开电梯门
}
private void button2_Click(object sender, EventArgs e)
{
ElevatorController ec = new ElevatorController(this.listBox1);
ec.WriteLog("...测试关闭电梯门...");
ec.SetCurrentState(ec.StateRunning);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorOpening);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorOpened);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorClosing);
ec.CloseDoor();//测试关闭电梯门
ec.SetCurrentState(ec.StateDoorClosed);
ec.CloseDoor();//测试关闭电梯门
}
private void button3_Click(object sender, EventArgs e)
{
ElevatorController ec = new ElevatorController(this.listBox1);
ec.WriteLog("...测试运行电梯...");
ec.SetCurrentState(ec.StateRunning);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorOpening);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorOpened);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorClosing);
ec.Run();//测试运行电梯
ec.SetCurrentState(ec.StateDoorClosed);
ec.Run();//测试运行电梯
}
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
电梯运行时状态
public class StateRunning : IElevatorState
{
private ElevatorController _ec;
public StateRunning(ElevatorController ec)
{
_ec = ec;
}
public void OpenDoor()
{
_ec.WriteLog("StateRunning.OpenDoor():[方法无效]不要命啦!电梯在升降中,不能开门!!!");
}
public void CloseDoor()
{
_ec.WriteLog("StateRunning.CloseDoor():[方法无效]电梯在运行,门已经是状态状态!!!");
}
public void Run()
{
_ec.WriteLog("StateRunning.Run():[方法无效]电梯已经在运行状态!!!");
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
关闭电梯门状态
public class StateDoorClosed : IElevatorState
{
private ElevatorController _ec;
public StateDoorClosed(ElevatorController ec)
{
_ec = ec;
}
public void OpenDoor()
{
_ec.WriteLog("StateDoorClosed.OpenDoor():正在关电梯门...");
_ec.SetCurrentState(_ec.StateDoorOpening);
}
public void CloseDoor()
{
_ec.WriteLog("StateDoorClosed.CloseDoor():[方法无效]:因电梯门已经是关闭状态!");
}
public void Run()
{
_ec.SetCurrentState(_ec.StateRunning);
_ec.WriteLog("StateDoorClosed.Run():正在启动电梯, 进入升降程序...");
if (_ec.IsUp)
_ec.WriteLog("StateDoorClosed.Run():电梯进入上升状态中...");
else if (_ec.IsDown)
_ec.WriteLog("StateDoorClosed.Run():电梯进入下降状态中...");
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
{
private ElevatorController _ec;
public StateDoorClosed(ElevatorController ec)
{
_ec = ec;
}
public void OpenDoor()
{
_ec.WriteLog("StateDoorClosed.OpenDoor():正在关电梯门...");
_ec.SetCurrentState(_ec.StateDoorOpening);
}
public void CloseDoor()
{
_ec.WriteLog("StateDoorClosed.CloseDoor():[方法无效]:因电梯门已经是关闭状态!");
}
public void Run()
{
_ec.SetCurrentState(_ec.StateRunning);
_ec.WriteLog("StateDoorClosed.Run():正在启动电梯, 进入升降程序...");
if (_ec.IsUp)
_ec.WriteLog("StateDoorClosed.Run():电梯进入上升状态中...");
else if (_ec.IsDown)
_ec.WriteLog("StateDoorClosed.Run():电梯进入下降状态中...");
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加微信
请下载附件!(Source for VIP)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网