Use enumeration state machine to achieve your elegant logic state change

welfare

Now focus on micro-channel public number: fat brother small farm yard, sending the keyword [lottery] to draw entities may have access to programming books. Activities only three days seize participate! [The draw] As of this weekend

Foreword

I believe many java developers wrote a status change of business processes such as order, leave processes and so on. We will engage in a general state identification to identify a stage of the life cycle. Many people will write this logic:

81c16edb01abf9a6905287d6d7c1465f.png

If the process dozens are we going to explode if. And this "0", "1" is several meanings?

Optimization approaches can of course use the design pattern in a state to carry out his model, allowing an object having a state of a package according to the different behavior of the same object in its state. We can convert between the programmed state and define separate state:

3a7712fe25ea715da4cce999f7e08372.png

But this operation will increase too much dependent on the state of the object. So there is no show does it work? Of course. We first learn to understand the state machine.

state machine

Full name of the state machine finite state machine, because the general state of the state machine are discrete and enumerable, which is limited reason. The state machine mathematical model that represents the behavior of a finite number of states as well as the transfer and movement between these and other states. Popular description defines a set of state machines that process more Bian state: the state machine comprises a set of states, defined when the state machine is in one state it can receive and perform the behavior event, after the execution is completed, the state machine in which the change of state may be perceived. Typically includes the following concepts:

  • I state too nonsense, no state can call the state machine state machine.
  • State Event state machine changes must be triggered by an event caused.
  • Behavior triggered after the execution of business logic. For example, change orders state to pay unpaid state business needs to go, write water, modify account balances.
  • A state change event is triggered to perform a certain behavior to reach a state of another process.

1786d6c5729de8738d2eeb8bb3fbbe49.png

The figure is a schematic process of change. The next thing to say another java enumerated.

Java enumeration

A Java enum is a special type of list of constants of one definition of class. It is a new feature introduced in JDK 1.5. Enumeration in the JDK in fact, has been designed as a singleton, it is not allowed to be instantiated externally, enumerated types of help are instantiated in its load JVM when we finished. This is the Java Virtual Machine Specification clearly defined to ensure the security thread. Since Java enum implicitly implement the enumeration superclass java.lang.Enum, and therefore can not be achieved another class, but can implement interfaces. You can declare abstract methods implemented by specific internal enumeration. We define a color enumeration to look at:

96946720c5e689dcefa9c25dc0501491.png

We can Colorful.RED.colorName()directly get the name of the color, very convenient and semantic.

Next, we will develop the actual scene to achieve a simple enumeration type of state machine for processing business.

Combat operations

Order delivery to receipt of this scenario, we simply consider the following scenario:

68f550216ebb615fe4a4d77b0d596054.png

Scheduled to undergo final delivery to the receipt of this simple process, we can be defined as a state enumeration:

91cfaad5231a51143a78e155d2c5c80c.png

Because the scheduling is the initial state, which method prevState point to themselves, so as to terminate its receipt status nextState point to themselves. Required to perform the process operation defined by the two pointers. Of course, the actual production to combine your own business to do.

Then we will be doing things like order flow (omitted getter and setter):

91cfaad5231a51143a78e155d2c5c80c.png

Let's look at a simple test:

8da6c5308d0105cc45ad62a3f9e8d421.png

After the transfer of the two, the goods reach the hands of the buyer's success, the state had the right to change, and maintainability has also been guaranteed only need to change the enumeration process. Well, here today, I hope a lot of attention.

Guess you like

Origin juejin.im/post/5d664467518825776118b555