FSM – implementation using the State pattern
Building on our switch-based approach, we will now refactor the BLE device connection FSM using the State design pattern. This pattern is “state-centric,” meaning each state is encapsulated as its own class. A common base class interface will allow the FSM to store pointers to these concrete state classes in a container.
In a typical FSM, states change dynamically at runtime in response to external interrupts and timers. In our example, we will continue using an enum to differentiate states and store the current one in a private member variable. This enum-based approach still works well with the State pattern, since it lets us quickly locate and switch between the concrete state objects that the FSM manages. We will start the implementation with the state class interface.
Understanding state class interfaces
The state class interface is shown in the following code:
class state {
public:
virtual ble_state...