We have constants declared in an interface in our application like this.
我们在应用程序的接口中声明了常量,就像这样。
public interface IConstants
{
public static final String FEVER="6";
public static final String HEADACHE="8";
}
We now want to populate these constants values (6 and 8) from the database (or application servlet context).
我们现在想要从数据库(或应用程序servlet上下文)填充这些常量值(6和8)。
The database values stored in a look up table are already available in the application session (in servlet context attribute) and hence I don't have to make a database call every time.
存储在查找表中的数据库值已在应用程序会话中可用(在servlet上下文属性中),因此我不必每次都进行数据库调用。
How do we accomplish this?
我们如何做到这一点?
5 个解决方案
#1
4
Given that you don't want to change the existing code too much, the simplest way would be:
鉴于您不想过多地更改现有代码,最简单的方法是:
public interface IConstants {
public static final String FEVER = getConstFromDatabase("FEVER");
public static final String HEADACHE = getConstFromDatabase("HEADACHE");
}
#2
1
Since the constant values are persisted in a database, the best approach would be to create an enum type and make them available through it.
由于常量值保存在数据库中,因此最好的方法是创建枚举类型并通过它使它们可用。
#3
1
Are you sure you need to?!
你确定需要吗?!
If you need to add new code when adding a new disease anyways, there is little point in making the "constants" to be data driven static (non-final) globals. (It will just complicate things.)
如果你在添加新疾病时需要添加新代码,那么使“常量”成为数据驱动的静态(非最终)全局变量几乎没有意义。 (这会让事情复杂化。)
-
If you are worried about a mismatch between the database and the code, you can verify that database and the constants match when starting up the application. You have the diseases defined in a table and are using some kind of referential integrity right?
如果您担心数据库和代码之间不匹配,则可以在启动应用程序时验证数据库和常量是否匹配。你有一个表中定义的疾病,并使用某种参照完整性吗?
-
If you DO think You need a data driven approach, you probably should not need any fields for "known diseases" at all, as the code shouldn't really depend on those. In that case each disease should be a proper object with a identity and other properties.
如果你认为你需要一种数据驱动的方法,你可能根本不需要任何“已知疾病”字段,因为代码不应该真正依赖于那些。在这种情况下,每种疾病都应该是具有身份和其他特性的适当对象。
-
If you need special handling for certain types you probably should go back to an enum again...
如果你需要对某些类型进行特殊处理,你可能应该再次回到枚举...
-
If you instead have many diseases (that might be added dynamically) and only a few types - several diseases are handled in the same way by the same code; add a type in the disease-table and in the code as an enum (or constant) for the type-of-disease and use that to run different logic.
如果您患有许多疾病(可能是动态添加的)并且只有少数几种类型 - 同一代码以相同的方式处理多种疾病;在疾病表和代码中添加一个类型作为疾病类型的枚举(或常量),并使用它来运行不同的逻辑。
-
If each disease actually have a lot of complexity, it might be a good idea to try to write one class for each disease. (You can then do a proper O/R thing with sub-classes and all...)
如果每种疾病实际上都有很多复杂性,那么尝试为每种疾病编写一个类别可能是个好主意。 (然后你可以用子类和所有......做一个正确的O / R事情。)
The interface enum cludge is not neccesary since java 5, as you can get both enums and do a static import if you need to.
从java 5开始,界面enum cludge不是必需的,因为你可以获得两个枚举并在需要时进行静态导入。
(And when using enum, validating the database is simpler as you get an enumeration for free.)
(当使用枚举时,验证数据库更简单,因为您可以免费获得枚举。)
Your fields should probably be located in a class called Disease or SickDude instead of a gigantic global constants class. (Like, for instance, the fields in Calendar). Possibly as an inner public enum in the class where they "belong."
你的字段可能应该位于一个名为Disease或SickDude的类中,而不是一个巨大的全局常量类。 (例如,日历中的字段)。可能是他们所属的班级中的公共内部词汇。
#4
0
To make them vary based on a data source, you'd need some kind of data structure for them.
为了使它们根据数据源而变化,您需要某种数据结构。
You could define a bean to hold the values (fields with getters/setters), or use a map for the values.
您可以定义一个bean来保存值(带有getter / setter的字段),或者使用map来表示值。
You should probably think of this as more like a configuration problem than a constants problem. Constants are really intended from a programming point of view.
您可能应该将此视为配置问题而不是常量问题。从编程的角度来看,常量实际上是有意的。
#5
0
As constants, you can't readily.
作为常数,你不能随便。
Create a class with those members, protect the setters (or set them from the constructor) and provide getter methods.
使用这些成员创建一个类,保护setter(或从构造函数中设置它们)并提供getter方法。
More detail:
更多详情:
Okay, to start with, you can't readily initialize constants from a database because they must be initialized at the time they're defined.
好的,首先,您无法从数据库中初始化常量,因为它们必须在定义时初始化。
Instead, create a class something like this:
相反,创建一个这样的类:
public class Constants {
private static final String FEVER ;
private static final String HEADACHE ;
public Constants(String fever, String headache){
if(FEVER == null){
FEVER = fever;
HEADACHE = headache;
} else {
// Do something; shouldn't be resetting these.
}
}
public String getFever(){ return FEVER; }
public String getHeadache(){ return HEADACHE; }
}
Now, when you construct the class, you set FEVER and HEADACHE. Since they're static, there's only one value; since you have only getters and they're private, no one can reset them; and since they're final, no one can sneak behind your back with inheritance.
现在,在构造类时,设置FEVER和HEADACHE。因为它们是静态的,所以只有一个值;既然你只有吸气剂,而且它们是私人的,没有人可以重置它们;因为他们是最终的,所以没有人可以继承你的背后。
#1
4
Given that you don't want to change the existing code too much, the simplest way would be:
鉴于您不想过多地更改现有代码,最简单的方法是:
public interface IConstants {
public static final String FEVER = getConstFromDatabase("FEVER");
public static final String HEADACHE = getConstFromDatabase("HEADACHE");
}
#2
1
Since the constant values are persisted in a database, the best approach would be to create an enum type and make them available through it.
由于常量值保存在数据库中,因此最好的方法是创建枚举类型并通过它使它们可用。
#3
1
Are you sure you need to?!
你确定需要吗?!
If you need to add new code when adding a new disease anyways, there is little point in making the "constants" to be data driven static (non-final) globals. (It will just complicate things.)
如果你在添加新疾病时需要添加新代码,那么使“常量”成为数据驱动的静态(非最终)全局变量几乎没有意义。 (这会让事情复杂化。)
-
If you are worried about a mismatch between the database and the code, you can verify that database and the constants match when starting up the application. You have the diseases defined in a table and are using some kind of referential integrity right?
如果您担心数据库和代码之间不匹配,则可以在启动应用程序时验证数据库和常量是否匹配。你有一个表中定义的疾病,并使用某种参照完整性吗?
-
If you DO think You need a data driven approach, you probably should not need any fields for "known diseases" at all, as the code shouldn't really depend on those. In that case each disease should be a proper object with a identity and other properties.
如果你认为你需要一种数据驱动的方法,你可能根本不需要任何“已知疾病”字段,因为代码不应该真正依赖于那些。在这种情况下,每种疾病都应该是具有身份和其他特性的适当对象。
-
If you need special handling for certain types you probably should go back to an enum again...
如果你需要对某些类型进行特殊处理,你可能应该再次回到枚举...
-
If you instead have many diseases (that might be added dynamically) and only a few types - several diseases are handled in the same way by the same code; add a type in the disease-table and in the code as an enum (or constant) for the type-of-disease and use that to run different logic.
如果您患有许多疾病(可能是动态添加的)并且只有少数几种类型 - 同一代码以相同的方式处理多种疾病;在疾病表和代码中添加一个类型作为疾病类型的枚举(或常量),并使用它来运行不同的逻辑。
-
If each disease actually have a lot of complexity, it might be a good idea to try to write one class for each disease. (You can then do a proper O/R thing with sub-classes and all...)
如果每种疾病实际上都有很多复杂性,那么尝试为每种疾病编写一个类别可能是个好主意。 (然后你可以用子类和所有......做一个正确的O / R事情。)
The interface enum cludge is not neccesary since java 5, as you can get both enums and do a static import if you need to.
从java 5开始,界面enum cludge不是必需的,因为你可以获得两个枚举并在需要时进行静态导入。
(And when using enum, validating the database is simpler as you get an enumeration for free.)
(当使用枚举时,验证数据库更简单,因为您可以免费获得枚举。)
Your fields should probably be located in a class called Disease or SickDude instead of a gigantic global constants class. (Like, for instance, the fields in Calendar). Possibly as an inner public enum in the class where they "belong."
你的字段可能应该位于一个名为Disease或SickDude的类中,而不是一个巨大的全局常量类。 (例如,日历中的字段)。可能是他们所属的班级中的公共内部词汇。
#4
0
To make them vary based on a data source, you'd need some kind of data structure for them.
为了使它们根据数据源而变化,您需要某种数据结构。
You could define a bean to hold the values (fields with getters/setters), or use a map for the values.
您可以定义一个bean来保存值(带有getter / setter的字段),或者使用map来表示值。
You should probably think of this as more like a configuration problem than a constants problem. Constants are really intended from a programming point of view.
您可能应该将此视为配置问题而不是常量问题。从编程的角度来看,常量实际上是有意的。
#5
0
As constants, you can't readily.
作为常数,你不能随便。
Create a class with those members, protect the setters (or set them from the constructor) and provide getter methods.
使用这些成员创建一个类,保护setter(或从构造函数中设置它们)并提供getter方法。
More detail:
更多详情:
Okay, to start with, you can't readily initialize constants from a database because they must be initialized at the time they're defined.
好的,首先,您无法从数据库中初始化常量,因为它们必须在定义时初始化。
Instead, create a class something like this:
相反,创建一个这样的类:
public class Constants {
private static final String FEVER ;
private static final String HEADACHE ;
public Constants(String fever, String headache){
if(FEVER == null){
FEVER = fever;
HEADACHE = headache;
} else {
// Do something; shouldn't be resetting these.
}
}
public String getFever(){ return FEVER; }
public String getHeadache(){ return HEADACHE; }
}
Now, when you construct the class, you set FEVER and HEADACHE. Since they're static, there's only one value; since you have only getters and they're private, no one can reset them; and since they're final, no one can sneak behind your back with inheritance.
现在,在构造类时,设置FEVER和HEADACHE。因为它们是静态的,所以只有一个值;既然你只有吸气剂,而且它们是私人的,没有人可以重置它们;因为他们是最终的,所以没有人可以继承你的背后。