You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
designPattern/DesignPattern/FSM/Machines/EquipmentStateMachine.cs

62 lines
1.8 KiB

using FSM.States;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FSM.Machines
{
public class EquipmentEventHandler
{
public IEquipmentState CurrentState { get; private set; }
public EquipmentEvent EquipmentEvent { get; private set; }
public string Message { get; private set; }
public EquipmentEventHandler(IEquipmentState currentState, EquipmentEvent equipmentEvent, string message)
{
CurrentState = currentState;
EquipmentEvent = equipmentEvent;
Message = message;
}
}
public class EquipmentStateMachine
{
private IEquipmentState _currentState;
public event EventHandler<EquipmentEventHandler> OnHandleEvent;
public EquipmentStateMachine(IEquipmentState initialState)
{
_currentState = initialState;
_currentState.Enter();
}
public void SetState(IEquipmentState newState)
{
_currentState.Exit();
_currentState = newState;
_currentState.Enter();
}
public void HandleEvent(EquipmentEvent equipmentEvent)
{
if (OnHandleEvent != null)
OnHandleEvent(this, new EquipmentEventHandler(_currentState, equipmentEvent, "HandleEvent called"));
bool handleResult = _currentState.HandleEvent(equipmentEvent, this);
if (OnHandleEvent != null)
{
string message;
if (handleResult)
message = "Success to handle the event";
else
message = "Fail to handle the event";
OnHandleEvent(this, new EquipmentEventHandler(_currentState, equipmentEvent, message));
}
}
}
}