The state machine uses Summary

background

A recent Invoicing and business write-off, which has more than N state. Think of a state machine model, reduction condition determining make clearer business logic. Back to increase or decrease the state, and he is able to quickly build a global view. Ideal is full, the reality is very skinny.

Technology selection Way

As a Java developer, the first thought is the Spring family of a state machine, after all, Spring is strong enough. After the meal, run up and caught it before finishing their basic expectations and demands, as follows:

  1. Business object itself has a status field, each state machine based on the field do initialization;
  2. Business Objects can receive event, whose field content should be easily expanded;
  3. Business transaction context state conversion process consistent strong;

Corresponds to Spring State Machi, later referred to SSM.

  1. initialization. SSM is based on the global state machine. After what is the global state machine, the state machine initializes i.e., while maintaining all the corresponding nodes and edges of objects, the subsequent service in a particular state transition. The official document also mentioned that the object belongs to the heavyweights, the proposed globally unique. Officially this reason, there is a problem in concurrent use, improper use can result in deadlocks. After the completion of the state transition, the need to achieve a particular persistent state of the interface state machine complete preservation.
  2. event. Event is based enumeration and annotations, this is the most ruthless. Enumerate the contents can not be modified. It only means that the event such a type of variable parameters.
  3. As for the state transition events expose the interface can only say that many do not have access to the powerful.
  4. Transaction boundaries difficult to control because there are unseen internal implementation of asynchronous tasks.

Under Summary SSM, a bit like a state machine engine, partial heavyweight, event objects represent rich enough, transaction control the level of personal reasons it can not guarantee.

The reason to develop their own schedule, refer to the Java state mode, defined StateContext and State. Each State only concerned about the current state of the incident support, and business logic corresponding event. Procedure as follows

  1. First come first serve state map that clearly state and transition event, complete with personal VSCode the markdown plug;
  2. One more state business table, indicating the state A, executed when the event is received business;
  3. YY run again, to complete the design;
  4. Optimization wave, business differences in each state are executed in different parameters, to mention a state as an abstract parent class status template. Each state increases as follows, whether to support an event that state support for all events.

Business Integration

Here, the state machine shell is complete, found another problem. What business in the state of Service, the Service in any business in general, what is the relationship between the two yes. Obviously, the status of the service should be placed in the state Service. Personally I think that the general state of business under the Service, should be nested, because the outside do not care how you achieve. If the mapper is 0 layer, Service layer is 1, and that the state service is 0.5 layer. After this issue is resolved, transaction control will be easier.

Test findings

  1. After using a state machine, the contents of the test itself and the state is divided into specific business, which is the template class method. Test test state machine as complicated If branch, but the branch of the state machine can easily exhausted, branch If I was not confident. If you are willing to put all the state transitions logs collected, you can also visualize the entire state transition process.

Review and Summary

  1. Design pattern is still very strong, not need to know the details, but the overall context. When needed, and then study the details.
  2. SSM may just want to enter a state and a state event to get a result, I have a question on usage. After the business logic in the execution state, it is also coupled. Ideally it should be loosely coupled way through the event listener, which is to learn from the SSM.
Released seven original articles · won praise 0 · Views 2694

Guess you like

Origin blog.csdn.net/weilaizhixing007/article/details/104646449