简体   繁体   English

使用方法保存不同类型的值

[英]Saving values of different types, with methods

I was to save some configuration values. 我要保存一些配置值。 :) These values are might be defined in a xml file and might be of the types String, boolean, String[], ... or others. :)这些值可能在xml文件中定义,并且可能是String,boolean,String [],...或其他类型。 These values might be defined in a xml file. 这些值可以在xml文件中定义。 Is there a best practise way to save these values, together with their tagname in the xml file and a default value. 是否有最佳实践来保存这些值,以及它们在XML文件中的标记名和默认值。 I want to clean up some ultra bad legacy code, where things look like this: 我想清理一些非常糟糕的旧代码,如下所示:

    public static final String VIEWCOOKIE_MAXLENGTH_VALUENAME = "view_cookie_max_length";
    public static final int VIEWCOOKIE_MAXLENGTH_DEFAULT = 512;
    public static int viewCookie_maxLength = VIEWCOOKIE_MAXLENGTH_DEFAULT;

    Integer temInt = basic.getTimedBaseIntegerValue(basic.c_adclear_section, null,
    CookieValues.VIEWCOOKIE_MAXLENGTH_VALUENAME,
    CookieValues.VIEWCOOKIE_MAXLENGTH_DEFAULT, currentTime);
if (firstLoad || basic.checkParamChanged(0,CookieValues.VIEWCOOKIE_MAXLENGTH_VALUENAME,
CookieValues.viewCookie_maxLength, temInt))
    CookieValues.viewCookie_maxLength = temInt;

those lines for all values, and methods for all the different type. 这些行用于所有值,方法用于所有不同类型。

Is there a better way like this: 有没有像这样更好的方法:

public class Value<T> {

   String valueName;
   final T defaultValue;
   T value;
   Method method;
    ...
}

but I don't know if this is the best way and also I'm a little bit worried, because I don't want to slow down the server, due to all the Wrapppers. 但是我不知道这是否是最好的方法,而且我有点担心,因为由于所有Wrapppers,我不想降低服务器的速度。 This problem is hard to search so I'm sorry if this has already been asked somewhere. 这个问题很难搜索,所以很抱歉是否已经有人问过这个问题。

This might answer your question: What is the best way to convert a java object to xml with open source apis 这可能会回答您的问题: 使用开源API将Java对象转换为xml的最佳方法是什么?

Basically you have to use a Marshaller (something that converts an object to text and back), and there are tons of those for Java. 基本上,您必须使用Marshaller(将对象转换为文本并返回文本的东西),而对于Java来说却有很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM