Constant Management of Java Architecture (Interface Definition Method) Using Interfaces to Define Constants (Beginners for Architects)

When developing java code, you often encounter many constants, and the following code may appear

    switch (userType){
      case 1:
        //1 is the administrator
        //to do something
        break;
      case 2:
        //2 is an ordinary member
        //to do something
        break;
      case 3:
        //3 is the tourist
        //to do something
        break;
    }

Judging the type of user, different user types are processed differently. There are comments here, so you can understand what is written. If there are no comments, you need to look at other codes or data to distinguish the userType. There are several types , divided into What is the meaning of the table, which seriously reduces the development efficiency, but this is often seen in normal development.

Follow me step by step to improve code quality

In the above example code, in the CheckStyle (everyone who writes java should understand this) specification, this writing method will also report a Magic Number warning. In this specification, only 0 and 1 can be directly written to the value, other values The definition must be mentioned inside the class as a static member variable . code similar to the following

public class Codetest {

  private static final int ADMIN = 1;
  private static final int USER = 2;
  private static final int VISITOR = 3;

  public static void main(String[] args) {
    int userType = ADMIN;
    switch (userType) {
      case ADMIN:
        //to do something
        break;
      case USER:
        //to do something
        break;
      case VISITOR:
        //to do something
        break;
    }
  }
}

How, so even if there are no comments, you can understand a little bit, and you can understand the meaning of the constants when you translate them into English, so that checkstyle will not report the MagicNumber warning.

However, what if different classes need to use the same variable, the same constant definition cannot be defined in each class, and there will be too much repetitive code, so all the constants are gathered together and a single constant is defined class, so we have the following code

public class Constant {
  public static final int ADMIN = 1;
  public static final int USER = 2;
  public static final int VISITOR = 3;
}

In this way, if you need to use constants, just use this class

However, the code can still be optimized, because there is a lot of public static final code in this and the class, we can avoid a lot of such definitions in other ways. This way is to use the interface.

public interface CodeConstant {

  /**
   * user type
   */
  interface USER_TYPE{
    int ADMIN = 1;
    int USER = 2;
    int VISITOR = 3;
  }

  /**
   * User login status
   */
  interface USER_STATUS{
    int LOGIN = 1;
    int LOGOUT = 2;
  }
}

It can be noticed that a large number of public static final are gone

Principle: define properties in the interface, will automatically add public static final

When we use it, we can call it like this~~

import cn.infol.service.webclient.rest.CodeConstant.USER_TYPE;

public class Codetest {

  public static void main(String[] args) {
    int userType = USER_TYPE.USER;
    switch (userType) {
      case USER_TYPE.ADMIN:
        //to do something
        break;
      case USER_TYPE.USER:
        //to do something
        break;
      case USER_TYPE.VISITOR:
        //to do something
        break;
    }
  }
}

This advantage is obvious, it is very convenient to define constants, and constants can be hierarchically structured

In dealing with general system development, this constant definition method has been able to meet our needs.

If your project is relatively large and the business logic is relatively complex, you will have some special operations on constants. For example, you need to find several types of constants under the USER_TYPE constant category. At this time, the interface definition method is not applicable. You can refer to My next article "Constant Management of Java Architecture (Enumeration Definition Method)"

Architectural point of view: The more advanced method is not necessarily the better, the advanced method will bring a certain amount of work while improving its own scalability. The method suitable for your own project is the best method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325159763&siteId=291194637