|
If you've ever wrestled with how to represent and manage the states of your PureMVC application, you'll be glad to know that an exciting new utility called StateMachine has been added to the PureMVC repositories.
The result of several weeks of collaboration between myself and the author Neil Manuell, this utility is an extremely powerful tool for formalizing the discrete states of your application in the simplest, most approachable form we could deliver. State is no longer the random basin of stability your app falls into after certain input is given and the app has responded. Nor is it something you'll have to jump through hoops to implement! Currently supporting AS3 Standard and MultiCore, the utility provides a simple yet effective Finite State Machine (FSM) implementation, which allows the definition of States, and the valid transitions to other States allowed from any given State, and the actions which trigger the transitions. Each State may also have associated entering and exiting Notifications, which allow the application to respond appropriately to the phases of a transition. Transitions can also be canceled, by notifying the StateMachine during the exiting phase of the transition, before the new State has been established. To make your application's FSM extremely easy to define, use and understand, a dependency injection mechanism is provided. FSMInjector reads the entire FSM, expressed in a simple XML format, and injects a fully populated StateMachine into the PureMVC app.
The XML format for the FSM Injector is simple. For instance here is the FSM for a lockable door: <fsm initial="CLOSED"> <state name="OPENED" entering="openingNote" exiting="aboutToCloseNote"> <transition action="CLOSE" target="CLOSED"/> </state> <state name="CLOSED" entering="closingNote"> <transition action="OPEN" target="OPENED"/> <transition action="LOCK" target="LOCKED"/> </state> <state name="LOCKED" entering="lockingNote"> <transition action="UNLOCK" target="CLOSED"/> </state> </fsm>
The above FSM defines three discrete States OPENED, CLOSED and LOCKED. - The actions OPEN, CLOSE and LOCK are used to trigger State transitions.
- It is only possible to LOCK the door when it is CLOSED, because only the CLOSED State defines a transition targeting the LOCKED state.
- It is not possible to OPEN the door from the LOCKED State because no transition is defined targeting the OPEN State.
- And when you UNLOCK the door, it returns to the CLOSED State, where it is once again possible to OPEN or LOCK.
- An exiting notification is defined only for exiting the OPEN State to illustrate that entering and exiting Notifications are optional.
- The CLOSED State's entering Notification ("closingNote") will be sent not only when transitioning to CLOSED from OPEN or LOCKED, but also when the StateMachine is first registered, since the FSM's initial State is CLOSED.
Download the StateMachine utility and check it out today! |