Before the enumeration to give an example, we all know that the published Csdn blog needs to be audited, then the status of the audit there are so many: audit, Audit pass, Audit does not pass (assuming that the current requirements are required to have so three states). So we can write that in the project.
if (state = = 1) { //1 represents the
MethodA () in the audit;
} else if (state = = 2) { //2 represents approval through
MethodB ();
} else if (state = = 3) { //3 indicates that the audit does not pass
methodc ();
}
But this kind of hard code is just a convenience, there are still a lot of problems. In case of the day need to change the status of the logo, with 0来 to express the audit does not pass, or demand changes, need to add, delete the state of the time, it will have to change all the state of the audit code, this workload is undoubtedly huge, if it is a huge project, there will be missed
Well, since this hard coding doesn't work, we can quickly think of a static constant (or a configuration file) to indicate the status of the audit
public static final int auditing = 1; Audit in public
static final int AUDIT = 2; Audit through public
static final int unaudit = 3; Audit does not pass
if (state = = auditing) {
MethodA ();
} else if (state = = AUDIT) {
methodb ();
} else if (state = = Unaudit) {
methodc ();
}
This is a good way to solve the problem of changing the status of identity, when the identity changes, simply modify the constant class on the line, without modifying the entire project all involved status identification. But when the requirements for a project change and you need to add an unaudited state, you need to add an else if to all the relevant code in addition to adding a variable to the constant class, which is obviously unscientific. Of course, you can refactor your code without using this if else to judge your logic. Method is still quite a lot, the following is a brief introduction to several.
1. Use polymorphism:
Base class:
abstract public class base{
abstract public void Method ();
Public
class Auditing extends base{public
void Method () {
...
}
} in Audit Audit passed public
class Audit extends base{public
void Method () {
...
}
} Auditing does not pass public
class Unaudit extends base{public
void Method () {
...
}
} Business functions public
static void Main (string[] args) {
//Get state from the database and assign to the variable '
base base = Match ';
Base.method ();
}
private static Base match (int state) {
if (state = = auditing) {return
new Auditing ();
} else if (state = = AUDIT) {return
new AUDIT ();
} else if (state = = Unaudit) {return
new Unaudit ();
}
}
When a new state is added, just add a specific logic to the match method without having to modify it in all the places involved. We can see that the above code is only suitable for a business logic, for example, in your project, all the judgments are based on the status of different colors: Blog status is audited, color display red, is approved by the color display black. But if there is a variety of business logic in your project, a logic is based on the status of different colors, a logic is based on the status of different information, then the above code does not apply, need to be slightly modified. Add parameters to method methods to invoke different business logic.
2. If polymorphism needs to create too many classes, you can use enumerations: Display different text and colors based on different states
Enum class public enum enumstate{Unmatch (-1, "Mismatch", "#FFFFFF"), Auditing (1, "Audit", "#FFFFFF"), AUDIT (2, "Audit passed", "#FFFFF
0 "), Unaudit (3," Audit Failed "," #FFFAFA ");
private int identify;
Private String Wordinfo;
private String color;
enumstate (int identify,string wordinfo,string color) {this.identify = identify;
This.wordinfo = Wordinfo;
This.color = color; public static enumstate getState (int state) {to (Enumstate eachstate:values ()) {if (state = EAC
hstate.identify) {return eachstate;
} return Unmatch;
public int getidentify () {return identify;
The public void setidentify (int identify) {this.identify = identify;
Public String Getwordinfo () {return wordinfo;
} public void Setwordinfo (String wordinfo) {this.wordinfo = Wordinfo;
Public String GetColor () {return color; } public VoiD setcolor (String color) {this.color = color; }//The actual business code obtains state enumstate enumstate = enumstate.getstate from the database;
As you can see from the above code, when the requirements change, we just need to make a simple change in the enumeration class, rather than modifying it in all the code involved.
In fact, there are many ways to circumvent the large number of if else caused by the problem, as to which method or look at your specific use of the scene . There is only one place in your project where if else is used, then why not use if else, and you know where to change it, anyway. Also some programmers do not like to use enumerations, like to use constants, I think the enumeration is also consuming performance, can not inherit can not expand, or constant use of simple and convenient, in the pursuit of code scalability, regardless of the type of security under the premise, then use Bai. Of course, if you need to change the constant value during the system upgrade phase, need to recompile, at this point with a constant is not as good as the configuration file or XML; on the contrary, some programmers do not like constants, like enumerations, and not a lot of places to use enumerations, then sacrifice a little performance for the maintenance of the code can not do it. As more and more enumerations are used, in view of the performance, it is necessary to use polymorphism or other methods to replace the enumeration
Well, now it's time to get to the point of using enumerations. Ha ha ha ...
public enum season{
spring,summer,fall,winter;
}
The season defined here is itself a class, and there are four instances (not strings) that can be printed directly season.spring the value comparison of two enumerated types of this instance can be directly used "= =" instead of having to use the equals
Season S1 = enum.valueof (Season.class, "SPRING");
Season s2 = enum.valueof (Season.class, "SPRING");
System.out.println (S1 = = s2); True
You can add constructors, methods, and fields to the enumeration class. Here, these four instances spring,summer,fall,winter have called four times the default constructor season ()
An enumeration class that is not abstract uses the final decoration by default, so an enumeration class cannot be inherited
Enumeration classes also cannot inherit other classes, because Java is a single inheritance, and he has inherited the Java.lang.Enum
The enumeration class is a single instance, so his constructor is private by default and must be private (can not be written)
All instances of the enumeration class must be listed in the first row, and the system will automatically use the public static final modifier
For the traversal of an enumeration class, you can use the values () method, Season []seasons = Season.values () to refer to the above code
ToString: Returns the name of an enumerated constant. Season.SPRING.toString () returns the string "SPRING"
Valueof:tostring Inverse method that changes a string to an enumeration type ordinal: Returns the position of an enumerated constant in an enumeration declaration, where the position is counted starting from 0. Season.SPRING.ordinal () return 0
There are other ways to refer to the API specifically.