Recently, Professor Zhou Ligong has published his long-awaited work "Programming and Data Structure" after years of effort. The electronic version of the book has been freely distributed to electronic engineers and college groups for download. With authorization from Professor Zhou Ligong, the content of this book is now being serialized.
> > > 1.1.1 Action Class
The previous section detailed the derivation process of the State pattern and provided a complete implementation. It also used simple print statements as examples of four different actions. However, in real-world applications, actions are likely to change depending on how they are executed directly within event handling methods. For instance, the card event handler for the LOCKED state is defined as:
```c
void locked_card(turnstile_t *p_turnstile)
{
turnstile_state_set(p_turnstile, &unlocked_state);
printf("unclock"); // Execute the unlock action
}
```
As you can see, any change in the action requires modifying the event handling method. To address this issue, it's recommended to encapsulate the gate actions into a dedicated action class, as illustrated in Figure 4.12.

In Listings 4.23 and 4.24, we show the declaration and implementation of the action class, even though the actions themselves seem simple. This approach is essential because only by anticipating transformations and managing changes can we build scalable and maintainable software.
Listing 4.23: Action Function Declarations (contents of turnstile_action.h file)
```c
#pragma once
void turnstile_action_lock(void);
void turnstile_action_unlock(void);
void turnstile_action_alarm(void);
void turnstile_action_thankyou(void);
```
Listing 4.24: Action Function Implementation (contents of turnstile_action.c file)
```c
void turnstile_action_lock(void)
{
printf("clock");
}
void turnstile_action_unlock(void)
{
printf("unclock");
}
void turnstile_action_alarm(void)
{
printf("alarm");
}
void turnstile_action_thankyou(void)
{
printf("thank you");
}
```
Each action—lock, unlock, alarm, and thank you—is implemented as a separate function. By separating these actions from the state machine, the logic of state transitions is cleanly decoupled from the actions performed by the FSM. This design allows for greater flexibility, enabling other FSMs with entirely different logic to use the same action interfaces without conflict.
Since no data is needed when processing an action, this becomes a method-only, stateless class. Therefore, there’s no need to define a specific type using a structure. However, if the action class were to include some data, it could be defined like this:
```c
typedef struct _turnstile_action {
// some data
} turnstile_action_t;
```
In such cases, changing the action would only require updating the corresponding function in the action class, without affecting the state machine’s event handlers. This modular design makes the system more robust and easier to maintain over time.
huasheng Siren Alarm,huasheng Siren Alarm for car,huasheng professional Siren Alarm
Gaoyou Huasheng Electronics Co., Ltd. , https://www.yzelechs.com