The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
java枚举实现状态机来优雅你的状态变更逻辑

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:

81c16edb01abf9a6905287d6d7c1465f.png

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:

3a7712fe25ea715da4cce999f7e08372.png

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.

1786d6c5729de8738d2eeb8bb3fbbe49.png

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:

96946720c5e689dcefa9c25dc0501491.png

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:
68f550216ebb615fe4a4d77b0d596054.png

Through the simple process of dispatch to dispatch and finally to receipt, we can define the following status enumeration:

91cfaad5231a51143a78e155d2c5c80c.png

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):

91cfaad5231a51143a78e155d2c5c80c.png

Let's take a simple test:

8da6c5308d0105cc45ad62a3f9e8d421.png

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.

read more
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/3146.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1

Comments0

美团代付 支持多模板全开源 多种支付通道 多模版三合一源码 附教程
Meituan payment supports multiple templates, full open source, multiple payment channels, multiple templates, three-in-one source code with tutorials
Someone bought it 6 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish