
preface
I believe that many java developers have written about status changes, such as order processes, leave processes, etc. Generally, a status indicator is created to identify a certain stage of the life cycle. Many people will write this logic:
If there are dozens of processes, wouldn't it be an explosion? And what do these "0" and "1" mean?
The optimization method can of course be done using the state pattern in the design pattern, allowing a stateful object to encapsulate different behaviors of the same object based on its state. We can program transitions between states and then define individual states:
However, this operation will increase excessive state object dependencies. So is there any dirty operation? Of course there is. Let's first understand the state machine.
state machine
The full name of a state machine is a finite state machine, because the states of general state machines are discrete and enumerable, which is the reason for the limitation. A state machine represents a mathematical model of a finite number of states and behaviors such as transitions and actions between these states. The popular description of a state machine is to define a set of state-changing processes: the state machine contains a set of states, which defines the events it can receive and the behaviors it can execute when the state machine is in a certain state. After the execution is completed, the state changes in the state machine can be perceived. Generally includes the following concepts:
- Status is too nonsense. I can still call a state machine without status.
- The state change of the event state machine must be caused by triggering an event.
- The behavior triggers the business logic that is then executed. For example, the business required to change the unpaid status of an order to the paid status, write down the flow, modify the account balance, etc.
- The process of changing one state triggered by an event to perform certain actions to reach another state.
The above figure is a schematic diagram of the change process. Next, it's time to talk about another thing: java enumeration.
Enumeration in Java
A Java enumeration is a special type of class that defines a list of constants. It is a new feature introduced in JDK 1.5. Enumerations are actually designed in a singleton mode in the JDK, so external instantiation of them is not allowed. The instantiation of enumeration types is completed by the JVM for us when they are loaded. This is clearly stipulated by the Java Virtual Machine Specification and ensures thread safety. Because Java enumeration implicitly implements the enumeration superclass java.lang.Enum, another class cannot be implemented, but an interface can be implemented. You can declare that abstract methods are implemented by concrete internal enumerations. Let's define a color enumeration to see:
we can Colorful.RED.colorName()
Getting the name of the color directly is very convenient and semantic.
Next, we will combine actual development scenarios to implement a simple enumeration-type state machine to handle business.
actual operation
For the scenario of order dispatch to receipt, we consider the following simple scenario:
Through the simple process of dispatch to dispatch and finally to receipt, we can define the following status enumeration:
Because scheduling is in the initial state, its prevenState method points to itself, and because it terminates when receiving goods, its nextState points to itself. Define these two pointers to perform the process required for the operation. Of course, actual production must be done in conjunction with your own business.
Then our order process can be done like this (omit getters and setters):
Let's take a simple test:
After two transfers, the goods successfully arrived in the buyer's hands, the status was correctly changed, and the maintainability was ensured. Only the enumeration process needed to be changed. Okay, that's all for today. I hope everyone will pay more attention.
Comments0