Java enumeration uses

Source: Internet
Author: User

0. Background

The scenario is this, the client to send the JSON data to the service-side resolution, due to the different versions of the client update, the data is different, the service side to be compatible with this difference to deal with this data differently, because the client upgrade is determined by the user.

1. Ideas

There are many ways to do it, just learn the enumeration in effective Java, enumeration can add a method, we this scene each log format corresponds to a method of parsing.

 Public enumlogversion {old (0), NEW (1) {@Override PublicString Parselog (jsonobject jsonobject) {returnJsonobject.tostring () + "new";    }    }; Private Final intnum; PrivateLogversion (intnum) {         This. num =num; }     PublicString Parselog (jsonobject jsonobject) {returnjsonobject.tostring (); }     Public Static voidMain (string[] args) {jsonobject jsonobject=NewJsonobject (); String Oldversionlog=Old.parselog (Jsonobject); String Newversionlog=New.parselog (Jsonobject);        System.out.println (Oldversionlog);    System.out.printf (Newversionlog); }}

Of course, the logic implemented in the project is much more complex than this. But would that be all right? The ultimate code is in the pursuit of: extensibility, reusability, performance. If the client sends another version, we can rewrite the parsing method in it, and when many different types of clients feel that the processing logic is very similar, we need to reuse this logversion processing, which will lead to problems, the first is to name our bad life, In addition, it is not so elegant to put different client logs in the same enum, and logically we should try to separate him. How do I extend this enum?

What is 1.0 enum?

An enum is a class, but this kind of compiler helps us do something extra:

1.enum will automatically inherit the Enum<t> class; Because Java does not support multiple inheritance, this class is declared as an enum and can no longer inherit other classes, so this is why enum cannot be extended with inheritance.

2.enum variable compiler will secretly in the front add public static final

1.1 How to extend an enum

According to the 1th of 1.0, we can only use the interface to expand, in order to achieve the logical structure of the code for clarity purposes.

Todo

Reference

https://www.zhihu.com/question/51987179/answer/128434410

Java enumeration uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.