In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "introduction and usage of enumerations in java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the introduction and usage of enumerations in java.
Preface
It is believed that many java developers have written about the business of status change, such as order process, leave process and so on. It is common to create a status identification to identify a stage in the life cycle. A lot of people would write this logic:
If there are dozens of processes, don't you want to if to explode. And what are the meanings of "0" and "1"?
Of course, the optimization method can be done by using the state pattern in the design pattern, which allows an object with state to encapsulate different behaviors of the same object according to its state. We can program transitions between states and then define individual states:
But this operation adds too many state object dependencies. So is there any coquettish operation? Of course there is. Let's first learn about the state machine.
State machine
The full name of the state machine is the finite state machine, because the states of the general state machine are discrete and enumerable, which is the reason for the limitation. A state machine represents a mathematical model of finite states and behaviors such as transitions and actions between them. The popular description of the state machine defines a set of state update processes: the state machine contains a set of states that defines the events and executable behaviors that the state machine can receive when it is in a certain state. The state changes in which the state machine is located can be perceived. It generally includes the following concepts:
The state is so nonsense. I can call a state machine without a state.
The state change of the event state machine must be caused by triggering an event.
The behavior triggers the business logic that is executed later. For example, the order does not pay the status of the change to the payment status of the business, write pipelining, modify the account balance and so on.
Changing a state is triggered by an event to perform a process in which some behavior reaches another state.
The picture above is a sign of the process of change. Next, it's time to talk about another thing: java enumeration.
Enumerations in Java
An enumeration of Java is a special type of class that defines a list of constants. Is a new feature introduced in JDK 1.5. Enumerations are actually designed as singleton patterns in JDK, so they are not allowed to be instantiated externally, and enumeration types are instantiated by JVM when they are loaded. This is clearly defined by the Java virtual machine specification and ensures the safety of threads. Because the Java enumeration implicitly implements the enumeration superclass java.lang.Enum, another class cannot be implemented, but the 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 get the name of the color directly through Colorful.RED.colorName (), which is very convenient and semantic.
Next, we will combine the actual development scenario to implement a simple enumerated type of state machine to handle the business.
Actual combat operation
For the scenario of order delivery to receipt, we consider the following simple scenarios:
After a simple process from scheduling to delivery to receiving, we can define the following status enumeration:
Because the scheduling is the initial state, its prevState method points to itself, and its nextState points to itself because the state is terminated at the time of receipt. The process required to perform the operation by defining these two pointers. Of course, the actual production should be combined with your own business.
Then our order process can be done like this (omitting getter and setter):
Let's have a simple test:
After two transfers, the goods successfully reach the buyer, the status is changed correctly, and the maintainability is guaranteed, only the enumeration process needs to be changed.
At this point, I believe you have a deeper understanding of "the introduction and usage of enumerations in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.