Java Android:如何将纯数据与资源分离?
我目前正在实现我的应用程序的数据层,只是想知道什么是将纯数据类/信息从相应的android资源(如字符串、颜色等)中分离出来的最佳方法 例如:Java Android:如何将纯数据与资源分离?,java,android,Java,Android,我目前正在实现我的应用程序的数据层,只是想知道什么是将纯数据类/信息从相应的android资源(如字符串、颜色等)中分离出来的最佳方法 例如: public final class Location { public static final int NOT_SPECIFIED = 0; public static final int AT_HOME = 1; public static final int AT_WORK = 2; public static f
public final class Location {
public static final int NOT_SPECIFIED = 0;
public static final int AT_HOME = 1;
public static final int AT_WORK = 2;
public static final int AWAY = 3;
public static final int[] VALUES = {
NOT_SPECIFIED, AT_HOME, AT_WORK, AWAY
};
@IntDef({ NOT_SPECIFIED, AT_HOME, AT_WORK, AWAY })
@Retention(RetentionPolicy.SOURCE)
public @interface Locations {}
private static String TXT_NOT_SPECIFIED;
private static String TXT_AT_HOME;
private static String TXT_AT_WORK;
private static String TXT_AWAY;
private Location() {}
/**
* Call once in Application.class to get a reference to the application context
* in order to load the string representations for the single locations.
*
* @param applicationContext the app´s context
*/
public static void init(Context applicationContext) {
Resources res = applicationContext.getApplicationContext().getResources();
/* Pre-load any string resources that correspond to locations. */
TXT_NOT_SPECIFIED = res.getString(R.string.text_location_not_specified);
TXT_AT_HOME = res.getString(R.string.text_location_at_home);
TXT_AT_WORK = res.getString(R.string.text_location_at_work);
TXT_AWAY = res.getString(R.string.text_location_away);
}
/**
* Returns the string representation of a given <code>location</code>.
*
* @param location must be in range of 0-3
*uhr
* @return the string representing the <code>location</code>
*/
public static String getStringForLocation(@Locations int location) {
switch (location) {
case NOT_SPECIFIED: return TXT_NOT_SPECIFIED;
case AT_HOME: return TXT_AT_HOME;
case AT_WORK: return TXT_AT_WORK;
case AWAY: return TXT_AWAY;
}
throw new IllegalStateException("'location' must be in range of 0-3"); /* Should never be called actually. */
}
类本身包含某些位置的纯数据表示(而不是使用enum
)。对于每个定义的位置
,在strings.xml
文件中都存储了一个字符串表示#
现在的问题是:我的应用程序的多个部分需要获取某个位置的名称。我不希望每次都在一个地方找到资源,这样如果有什么变化(添加了新的位置
选项等),我只需要在一个地方触摸代码
目前我已经实现了它,如上面的代码所示:Location
类有一个静态init方法,它在应用程序的onCreate中被调用,以获取上下文
,以便加载字符串解析。(我知道我本可以在上下文中作为getStringForLocation
方法的参数传递,但我不能决定哪一个更好?)
然而,通过这种方式,在我假定的纯数据类中有android依赖项。
在保持将这些数据映射到一个地方的资源的代码的同时,分离这些依赖关系的好方法是什么?在进行映射的数据层之外有一个静态util方法吗 我不太明白为什么要在某些变量中预加载字符串。我的理解是,从资源中获取字符串在性能方面并不昂贵
我会用这样的东西:
public static String getLocationString(Context context, @Locations int location) {
switch (location) {
case NOT_SPECIFIED: return context.getString(R.string.text_location_not_specified);
case AT_HOME: return context.getString(R.string.text_location_at_home);
case AT_WORK: return context.getString(R.string.text_location_at_work);
case AWAY: return context.getString(R.string.text_location_away);
}
throw new IllegalStateException("'location' must be in range of 0-3"); /* Should never be called actually. */
}
从一项活动中:
String myLocation = Location.getLocationString(this, Location.AT_HOME);
而且你可以去掉Location.init
方法为什么不使用enum
?据我所知,这不是android系统的首选方法,因为内存或性能原因。Android程序员建议在注释IntDef中使用整数常量。至少我是这样理解的。[好吧,我同意,这会让它更简洁。我不知道获取资源会有多大影响,所以我认为在应用程序开始时加载它们是一个好主意。关于代码的放置,您会将其放在Location类中还是放在其他地方?谢谢。我通常有一个MyAppHelper类在mypackage.utils文件夹中,我在其中添加了所有共享常量和函数,就像那个一样。MyAppHelper.getLocationString(context,Location.AT_HOME)
,等等……好的,这就是我的意思。谢谢你的回答:)