Correct use of enumeration - java

Today's topic is enumeration. The content is very basic. Let's start with a storyline, and finally lead to the benefits of using enumeration.

Requirement: There is one 任务(Task), the server defines a int statusstate to mark the task, including  three  states, 0, 1, and 2, respectively 未开始. After we get this , we need to display different texts according to different states (requirements are still being mined).进行中已完成status

The needs are clear, and generally we do.

Define three static constants in the global constant class to represent state constants:

static constant

Next, strings.xmldefine the state value: 未开始, 进行中,已完成

status value

Finally, after getting it in the code status, we do this:

switch-1

In this way, all the states and state values ​​are unified, the specifications are very good, the effect is also good, and there is no sense of violation!

Effect -1

Suddenly one day, the product Wang found you: Hi, let's set the color in front of the text to be dynamic , the color in different states is different, you can change it according to the rendering...". Fortunately, this is a simple, minute-to-minute matter.

In colors.xml, add several color values:

colors

Then modify the switch statement so that:

switch-2

So easy, quickly solved the problem!

Slowly, with the deepening of development, you find that many places need to be displayed in this way, so you encapsulate a custom ViewGroup, and everything is developing in a good direction. But in addition, in other places, it is also necessary to judge the value of this state, in order to obtain the attributes in this state, in case one day the product Wang tells you: "I want to add one more state, unknown (UN_KONW). The server side It has been synchronized with iOS, hurry up here", which means that all the previous state judgments need to be found and added, aren't you dumbfounded?

Therefore, most people still choose to write this part into the global static method, so that adding fields and states is no longer afraid. Add a few constants [status, color, status text] to the corresponding file, and change the method. That's it! Since this is not the focus of this article, I will not post the code.

So far, our program has been perfected step by step, and some basic routines can no longer stumped us, perfect!

The function is basically implemented, but in retrospect, this is not elegant! On the one hand, we still need to change a lot of places when the demand changes, on the other hand, we can't intuitively see 状态值status与状态文本、色值the relationship between them, for example  0代表未开始,颜色为黄色. Of course, you can solve this problem by relying on naming conventions as above. However, based on the naming convention, is there a better way to further strengthen the relationship between them? Of course there is! Next, on to today's topic, let's use enumeration to complete this task.

——————–Enumeration debut —————————–

At the beginning, we can define an enumeration class and write it like this (if you are not familiar with this way of writing, you can check other blog posts):

enum -1

It's not too simple to use (status is the status value obtained from the server):

enum-2

Then later the product said to add a color value, and you added it calmly. Write this:

enum-3

It doesn't matter when you use it:

enum-4

The point is here, it's time to add the 未知(UN_KNOW) state. According to the above method, we have to change and add a lot of things, but now we only need to add one line of code, yes, one line of code :

write picture description here

It's that simple! At the same time, if you write it like this, you don't need  to add a bunch of values ​​to the string.xml, , colors.xmland constant classes. It is very intuitive to put them directly in the enumeration class. Is there any way? !

Dissatisfied?

Of course, sometimes we need to use a constant somewhere to judge, the following figure is a common practice. Now that we write the state value into the enumeration, can it still be used so intuitively? The answer is yes!

enum-5

OK, that's it for the sharing of using enums instead of constants. The full text is developed in the form of a story line. So much is said for this knowledge point, which shows that it is still very important in some scenarios.

Finally, let me state that what I said about replacing constants with enumerations is for situations like "there is an association between constants". For example 状态值status与状态文本、色值, using enumerations at this time can greatly simplify our work, not to say that all constants will be replaced in the future. Written as an enumeration, after all, the official use of enumeration is not recommended :

Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

But just try to avoid it! Therefore, in actual development, it is necessary to consider according to the actual use scenario to prevent abuse. Like the scene described in this article, it is recommended to use it, and the combat power is doubled!

Please indicate the source for reprint: ( http://blog.csdn.net/My_TrueLove/article/details/52074493 )

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326559766&siteId=291194637