STATE mode technology

zhaozj2021-02-11  164

1st origin

On May 19, 2004, the department held the technical exchange activities of OO design ideas and excellent development tools on the 9th floor of Building 2. At the meeting, colleagues The main content of the two topics, respectively, "State mode" and "DUnit unit test tool". In the process of lectures and technical exchanges in the previous topic, everyone was discussed. I also have some feelings and experiences, hereby records, readers.

2 scenario

Take the cross door as an example.

The above is the state migration map of the cross transfer (UML). The format of the figure has the following characteristics:

1. Express possible all states with rounded rectangles. Note that the state should adopt adjective expression;

2. Use the line segment of the single arrow to express migration, and migrate from the original state to the destination status. The text on the migration line contains two parts of the event and action and uses "/" segmentation.

Comprehensive map you know:

There are two states in the N cross transfer, which are Locked and Unlocked;

There are two incidents in the N cross transmissions, respectively, PASS and COIN.

n With the processing of events through each state, you can get four actions, which are setlocked, setunlocked, Alarm, Thank you. Among them, ALARM and THANK YOU can be seen as a process for abnormal conditions.

3 initiative

After simply considering, Case is used to implement the above features, such as:

TSTATE = (stlocked, stunlocked);

TEVENTTYPE = (ETPASS, ETCOIN);

...

Case fstate of

STLOCKED:

Case aeventtype of

Etpass:

Doalarm;

Etcoin:

Dosetunlocked;

STUNLOCKED:

Case aeventtype of

Etpass:

Dosetlocked;

Etcoin:

Dothankyou;

END;

It can be seen that the above code has completed the function through the hierarchy of the CASE statement. Features Although it has been implemented, we seem to have a little enough! Is it too cumbersome? Imagine that if there are more, there are more events, and the corresponding CASE statement is not to write longer? And different states are set together for different events, causing unnecessary correlation, and also cause more erroneous opportunities for code maintenance (it is undeniable, there is also a benefit of logical concentration: Easy to read code).

4 mode implementation

The above problem is used to implement the State mode, that is, another style and realm:

Carefully study the above diagram, it is not difficult to find:

1. TturnStile points to a specific state class representing its status;

2. Abstract the status and all events as an abstract (pure virtual) method, then derive each event for each state;

3. I know the relationship: the door class knows the status abstraction class, and all specific state classes; the specific state class is not a member variable, nor does the door class. The specific state class does not know each other; the specific state class simply knows the instance of the door class corresponding to the attendance from the parameter of the coverage method;

4. Each specific state class can be implemented in a single way;

The advantages of the above solution are reflected in:

1. Separate state and control, two lines independently evolved, and the system's scalability is enhanced; There is no knowledge between the state, lowering their coupling;

3. Using class names and event names, the original two-layer case statement is eliminated.

Another problem worth thinking:

What should the state transition and the execution of the action should be done? In this example, I think the status class should be responsible for it. Because if the category is completed, the application of this mode is not really in place.

The responsibilities of the door class: Store the current state; forwarding events; performs open / closing movements; switch status according to requirements;

The true switching of the state is triggered by the state class - in this sense, the state is still coupled-only such coupling is not a direct reference relationship, but is performed by a logical relationship.

5 related patterns

The mode is for a particular situation, suitable for solving specific problems under certain scenarios. In general, the pattern can always bring more benefits, but at the same time, it will often bring some defects or regrets. How to pay, specific questions should be specific. Its aim is how to make the software system extensibility, better maintainability, simple, easy to understand.

6 other

6.1 About Database Persistence Framework

转载请注明原文地址:https://www.9cbs.com/read-5301.html

New Post(0)