画出动作转换图为:
使用模块化设计,将起落杆、出入传感器和通行灯设计成四个模块,分别继承设计好的系统模块接口:
1 //FSM_Interface.h 2 #pragma once 3 4 namespace FSM 5 { 6 7 class ISystemUnit //系统单元接口 8 { 9 public: 10 11 virtual void Initialize() = 0; //初始化 12 13 virtual void Execute() = 0; //执行动作 14 15 virtual void SetState(bool state) = 0; //更改状态 16 17 virtual bool GetState() const = 0; //获得当前状态 18 19 virtual void Shutdown() = 0; //关闭 20 21 protected: 22 23 bool State; 24 }; 25 }
1 //FSM.h 2 #pragma once 3 4 #include "FSM_Interface.h" 5 #include <iostream> 6 7 using std::cout; 8 using std::endl; 9 10 #define Up true 11 #define Down false 12 #define Green true 13 #define Red false 14 #define CarIn true 15 #define CarOut false 16 17 #ifdef _IOSTREAM_ 18 #define ErrorReport(ERR) 19 { 20 cout << #ERR << endl; 21 } 22 23 #define StateReport(STATE) 24 { 25 cout << #STATE << endl; 26 } 27 #else 28 #define ErrorReport(ERR) 29 #define StateReport(STATE) 30 #endif 31 32 namespace FSM 33 { 34 class ControlSystem sealed //控制系统类 35 { 36 public: 37 38 void SystemInitialize(); //系统初始化 39 40 void SystemShutdown(); //系统关闭 41 42 void Update(bool CarState); //根据车的进入或进出状态更新系统状态 43 44 static ControlSystem* GetInstance(); //单例模式 45 46 private: 47 48 static ControlSystem* pInstance; //指向该单例的指针 49 50 ControlSystem(); //不允许显式创建对象 51 ~ControlSystem(); 52 }; 53 54 class LiftLever :public ISystemUnit //栏杆类 55 { 56 public: 57 58 static LiftLever* GetInstance(); 59 60 void Initialize(); 61 62 void Shutdown(); 63 64 void Execute(); 65 66 void SetState(bool state); 67 68 bool GetState() const; 69 70 private: 71 72 bool State = Down; 73 74 static LiftLever* pInstance; 75 76 LiftLever(); 77 ~LiftLever(); 78 }; 79 80 class InSensor :public ISystemUnit //入闸传感器类 81 { 82 public: 83 84 static InSensor* GetInstance(); 85 86 void Initialize(); 87 88 void Shutdown(); 89 90 void Execute(); 91 92 void SetState(bool state); 93 94 bool GetState() const; 95 96 private: 97 98 bool State = false; 99 100 static InSensor* pInstance; 101 102 InSensor(); 103 ~InSensor(); 104 }; 105 106 class OutSensor :public ISystemUnit //出闸传感器类 107 { 108 public: 109 110 static OutSensor* GetInstance(); 111 112 void Initialize(); 113 114 void Shutdown(); 115 116 void Execute(); 117 118 void SetState(bool state); 119 120 bool GetState() const; 121 122 private: 123 124 bool State = false; 125 126 static OutSensor* pInstance; 127 128 OutSensor(); 129 ~OutSensor(); 130 }; 131 132 class Light :public ISystemUnit //信号灯类 133 { 134 public: 135 136 static Light* GetInstance(); 137 138 void Initialize(); 139 140 void Shutdown(); 141 142 void Execute(); 143 144 void SetState(bool state); 145 146 bool GetState() const; 147 148 private: 149 150 bool State = Red; 151 152 static Light* pInstance; 153 154 Light(); 155 ~Light(); 156 }; 157 158 }
1 //FSM.cpp 2 #include "FSM.h" 3 4 namespace FSM 5 { 6 ControlSystem* ControlSystem::pInstance = nullptr; 7 8 ControlSystem * ControlSystem::GetInstance() 9 { 10 if (pInstance == nullptr) 11 { 12 pInstance = new ControlSystem(); 13 } 14 return pInstance; 15 } 16 17 void ControlSystem::SystemInitialize() 18 { 19 LiftLever::GetInstance()->Initialize(); 20 InSensor::GetInstance()->Initialize(); 21 OutSensor::GetInstance()->Initialize(); 22 Light::GetInstance()->Initialize(); 23 } 24 25 void ControlSystem::SystemShutdown() 26 { 27 Light::GetInstance()->Shutdown(); 28 OutSensor::GetInstance()->Shutdown(); 29 InSensor::GetInstance()->Shutdown(); 30 LiftLever::GetInstance()->Shutdown(); 31 } 32 33 ControlSystem::ControlSystem() 34 { 35 } 36 37 ControlSystem::~ControlSystem() 38 { 39 } 40 41 LiftLever* LiftLever::pInstance = nullptr; 42 43 LiftLever* LiftLever::GetInstance() 44 { 45 if (pInstance == nullptr) 46 { 47 pInstance = new LiftLever(); 48 } 49 return pInstance; 50 } 51 52 void LiftLever::Initialize() 53 { 54 } 55 56 void LiftLever::Shutdown() 57 { 58 if(pInstance) 59 delete pInstance; 60 } 61 62 void LiftLever::SetState(bool state) 63 { 64 State = state; 65 } 66 67 bool LiftLever::GetState() const 68 { 69 return State; 70 } 71 72 LiftLever::LiftLever() 73 { 74 } 75 76 LiftLever::~LiftLever() 77 { 78 } 79 80 InSensor* InSensor::pInstance = nullptr; 81 82 InSensor* InSensor::GetInstance() 83 { 84 if (pInstance == nullptr) 85 { 86 pInstance = new InSensor(); 87 } 88 return pInstance; 89 } 90 91 void InSensor::Initialize() 92 { 93 } 94 95 void InSensor::Shutdown() 96 { 97 if (pInstance) 98 delete pInstance; 99 } 100 101 void InSensor::SetState(bool state) 102 { 103 State = state; 104 } 105 106 bool InSensor::GetState() const 107 { 108 return State; 109 } 110 111 InSensor::InSensor() 112 { 113 } 114 115 InSensor::~InSensor() 116 { 117 } 118 119 OutSensor* OutSensor::pInstance = nullptr; 120 121 OutSensor* OutSensor::GetInstance() 122 { 123 if (pInstance == nullptr) 124 { 125 pInstance = new OutSensor(); 126 } 127 return pInstance; 128 } 129 130 void OutSensor::Initialize() 131 { 132 } 133 134 void OutSensor::Shutdown() 135 { 136 if (pInstance) 137 delete pInstance; 138 } 139 140 void OutSensor::SetState(bool state) 141 { 142 State = state; 143 } 144 145 bool OutSensor::GetState() const 146 { 147 return State; 148 } 149 150 OutSensor::OutSensor() 151 { 152 } 153 154 OutSensor::~OutSensor() 155 { 156 } 157 158 Light* Light::pInstance = nullptr; 159 160 Light* Light::GetInstance() 161 { 162 if (pInstance == nullptr) 163 { 164 pInstance = new Light(); 165 } 166 return pInstance; 167 } 168 169 void Light::Initialize() 170 { 171 } 172 173 void Light::Shutdown() 174 { 175 if (pInstance) 176 delete pInstance; 177 } 178 179 void Light::SetState(bool state) 180 { 181 State = state; 182 } 183 184 bool Light::GetState() const 185 { 186 return State; 187 } 188 189 Light::Light() 190 { 191 } 192 193 Light::~Light() 194 { 195 } 196 }
在FSM_Execute.cpp中实现系统响应和模块动作:
1 //FSM_Execute.cpp 2 #include "FSM.h" 3 4 namespace FSM 5 { 6 void ControlSystem::Update(bool CarState) 7 { 8 //-----------检测到汽车进入,入闸传感器置为true-----------// 9 //------------------触发入闸传感器执行动作---------------// 10 if (CarState == CarIn) 11 { 12 StateReport(Car_In); 13 if ((LiftLever::GetInstance()->GetState() == Down) 14 && (Light::GetInstance()->GetState() == Red)) 15 { 16 InSensor::GetInstance()->SetState(true); 17 StateReport(InSensor_Set_True); 18 InSensor::GetInstance()->Execute(); 19 } 20 else 21 { 22 ErrorReport(Check_State_Of_LiftLever_And_Light); 23 } 24 } 25 //-----------检测到汽车出闸,出闸传感器置为true-----------// 26 //------入闸传感器置为true,触发出闸传感器执行动作--------// 27 else 28 { 29 StateReport(Car_Out); 30 if ((LiftLever::GetInstance()->GetState() == Up) 31 && (Light::GetInstance()->GetState() == Green)) 32 { 33 InSensor::GetInstance()->SetState(false); 34 OutSensor::GetInstance()->SetState(true); 35 StateReport(InSensor_Set_False); 36 StateReport(OutSensor_Set_True); 37 OutSensor::GetInstance()->Execute(); 38 } 39 else 40 { 41 ErrorReport(Check_State_Of_LiftLever_And_Light); 42 } 43 } 44 } 45 46 void LiftLever::Execute() 47 { 48 if (InSensor::GetInstance()->GetState() == true) //入闸和出闸时栏杆的执行动作不同 49 { 50 if (this->State == Up) //检查状态是否被正确切换,下同 51 { 52 Light::GetInstance()->SetState(Green); 53 StateReport(Light_Set_Green); 54 Light::GetInstance()->Execute(); 55 } 56 else 57 { 58 ErrorReport(LiftLever_May_Brokendown); 59 } 60 } 61 else if (OutSensor::GetInstance()->GetState() == true) 62 { 63 if (this->State == Down) 64 { 65 Light::GetInstance()->SetState(Red); 66 OutSensor::GetInstance()->SetState(false); 67 StateReport(Light_Set_Red); 68 StateReport(OutSensor_Set_False); 69 } 70 else 71 { 72 ErrorReport(LiftLever_May_Brokendown); 73 } 74 } 75 } 76 77 void InSensor::Execute() 78 { 79 if ((LiftLever::GetInstance()->GetState() == Down) 80 && (this->State == true)) 81 { 82 LiftLever::GetInstance()->SetState(Up); 83 StateReport(LiftLever_Up); 84 LiftLever::GetInstance()->Execute(); 85 } 86 else 87 { 88 ErrorReport(Check_State_Of_LiftLever_Or_The_InSensor_May_Brokedown); 89 } 90 } 91 92 void OutSensor::Execute() 93 { 94 if ((this->State == true)) 95 { 96 LiftLever::GetInstance()->SetState(Down); 97 StateReport(LiftLever_Down); 98 LiftLever::GetInstance()->Execute(); 99 } 100 else 101 { 102 ErrorReport(OutSensor_May_Brokendown); 103 } 104 } 105 106 void Light::Execute() 107 { 108 if ((this->State == Green) 109 && (LiftLever::GetInstance()->GetState() == Up)) 110 { 111 ControlSystem::GetInstance()->Update(CarOut); //在栏杆升起且灯变绿后汽车 112 } //可通行,更新系统状态 113 else 114 { 115 ErrorReport(Light_May_Brokendown); 116 } 117 } 118 }
以下为测试程序:
1 //main.cpp 2 #include "FSM.h" 3 4 using namespace FSM; 5 6 int main(void) 7 { 8 ControlSystem::GetInstance()->SystemInitialize(); 9 ControlSystem::GetInstance()->Update(CarIn); 10 ControlSystem::GetInstance()->SystemShutdown(); 11 system("pause"); 12 return 0; 13 }
测试程序运行结果: