Enumeration classes and dictionaries

In general, if a large number of state exist in the system constant value, it represents a unique identifier such as seasons code, respectively SPRING, SUMMER, FALL, WINTER, we can build a dictionary table or use an enum class.
If stored in the dictionary table of the database, store the codevalue and Chinese name, and define the access method and interface.
If the enumeration class also requires the use of codeand nametwo properties, the class can be defined in the enumeration of all acquired enumeration methods. Such as:

import java.util.ArrayList;
import java.util.List;

public enum SeasonEnum {

   SPRING("春天","SPRING"),
   SUMMER("夏天","SUMMER"),
   FALL("秋天","FALL"),
   WINTER("冬天","WINTER");

   private final String name;
   private final String code;
   private SeasonEnum(String name,String code)
   {
       this.name = name;
       this.code=code;

   }

   public String getName() {
       return name;
   }

   public String getCode() {
       return code;
   }
   
   public List<EnumDO> getAll(){
       List<EnumDO> enumDOS=new ArrayList<>();
       for (SeasonEnum seasonEnum:SeasonEnum.values()){
           EnumDO enumDO=new EnumDO();
           enumDO.setCode(seasonEnum.getCode());
           enumDO.setCode(seasonEnum.getCode());
           enumDOS.add(enumDO);
       }
       return enumDOS;
   }
}

Since these two methods can be used, what is the difference?
Use enumeration class does not need to look up the table, the speed is faster, but if the state constant value changes, the code in the enumeration class needs to be changed and repackaged and released. If there is no need to change the code in the database, just write SQL to add check and change.

Guess you like

Origin blog.csdn.net/weixin_43213064/article/details/109704852