welfare

Now pay attention to the wechat public number: code farmer Xiao Pangge, send the keyword [lottery] for the lottery, you can have the opportunity to obtain physical programming books. There are only 3 days left to participate! [The lucky draw ends at the end of this week]

preface

I’m sure many Java developers have written about state change businesses, such as order flow, leave flow, and so on. A status identifier is usually used to identify a phase of the lifecycle. Many people write this logic:

If the flow of dozens of not if to the explosion. And this “0”, “1” is how many meanings?

Optimization can of course be done by using the state pattern in design patterns, which allows 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:

But this operation adds too many state object dependencies. So there is no SAO operation? B: of course. Let’s take a look at the state machine.

The state machine

State machines are known as finite state machines, because the states of general state machines are discrete and enumerable, which is why they are finite. A state machine is a mathematical model of behavior that represents a finite number of states and the transitions and actions between these states. The popular description of a state machine is a set of states that define the variations in state that the state machine may receive and perform as the device is in a set of states. It generally contains the following concepts:

  • States are bullshit. I mean, a state machine without a state would be a state machine.
  • A state change in an event state machine must be caused by firing an event.
  • The behavior triggers the business logic that is later executed. For example, the change from the unpaid status to the paid status of the order needs to take the business, write the flow, modify the account balance and so on.
  • To change the process by which a state is triggered by an event to perform some action to reach another state.

The diagram above is an illustration of the process of change. It’s time for another thing, Java enumerations.

Enumerations in Java

A Java enumeration is a special type of class that defines a list of constants. Is a new feature introduced in JDK 1.5. Enumerations in the JDK are designed to be singletons, so they are not allowed to be instantiated externally. Enumerations are instantiated by the JVM when they are loaded. This is specified by the Java Virtual Machine specification and ensures thread safety. Because Java enumerations implicitly implement the enumeration superclass java.lang.enum, you cannot implement another class, but you can implement interfaces. You can declare that abstract methods are implemented by concrete internal enumerations. Let’s define a color enumeration to see:

We can directly get the colorName through colorful.red. ColorName (), which is very convenient and semantic.

Next, we will implement a state machine of simple enumeration types to process the business in a real development scenario.

Practical operation

For the order delivery to receipt scenario, we consider the following simple scenario:

Through the simple process of dispatching to dispatching and finally to receiving, we can define the following enumeration of states:

Since scheduling is the initial state, its prevState method points to itself, and its nextState points to itself because it terminates when receiving goods. Define these two Pointers to carry out the flow required for the operation. Of course the actual production should be combined with your own business to do.

Then our order flow can do this (omitting the getter and setter) :

Let’s have a quick test:

After two rounds of circulation, the goods are successfully delivered to the buyer, the state is changed correctly, and the maintainability is guaranteed, just need to change the enumeration process. Well, that’s all for today. I hope you pay more attention.