Java - DAO 不同类型的相同枚举
Java - Same Enum for different Types for DAO
我正在尝试构建一个可以处理不同设置类型的 DAO。我想知道是否有一种巧妙的方法可以做到这一点而不会出现运行时错误。
public interface ChannelSettingDAO {
Integer getIntegerSetting(ChannelSettingInteger channelSettingInteger);
String getStringSetting(ChannelSettingString channelSettingString);
Double getDoubleSetting(ChannelSettingDouble channelSettingDouble);
void setIntegerSetting(ChannelSettingInteger channelSettingInteger, Integer value);
void setStringSetting(ChannelSettingString channelSettingString, String value);
void setDoubleSetting(ChannelSettingDouble channelSettingDouble, Double value);
}
public enum ChannelSettingInteger {
CHANNEL_LOOKBACK(50);
private Integer defaultValue;
ChannelSettingInteger(Integer defaultValue) {
this.defaultValue = defaultValue;
}
public Integer getDefaultValue() {
return defaultValue;
}
}
etc.. for every type of enum.
有没有更简洁的方法来做到这一点。我觉得我错过了一些东西,可能是某种为枚举赋予类型的方法,或者我错过了一些模式。
至少有一种方法可以强制 getDefault 名称相同。
有什么建议吗?
Is there any neater way to do this. I feel like I'm missing out on something, some way to give a type to an enum maybe, or some pattern I've missed out on.
At least a way to enforce that the getDefault name is the same.
你是对的,你可以使用另一种设计来更好地满足你的需求。
您可以引入一个接口,每个枚举都必须实现该接口才能拥有 getDefault()
方法。通过这种方式,您可以确保每个枚举具有相同的基类型(接口中的一个)并且自从在接口中声明后就提供了 getDefault()
。
通过在接口上使用通用类型,您允许每个枚举都有自己的 getDefaultValue
数据类型
示例代码:
public interface IChannelSetting<T> {
public T getDefaultValue();
}
public enum ChannelSettingInteger implements IChannelSetting<Integer> {
CHANNEL_LOOKBACK(50);
private Integer defaultValue;
ChannelSettingInteger(Integer defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public Integer getDefaultValue() {
return defaultValue;
}
}
我不知道你将如何使用你的 DAO,但只是为了你的个人信息,如果与你的需要相关,你可以进一步利用公共基础接口来获得对称逻辑和更少的代码在你的 DAO 中。
事实上,您可以在 DAO 中只声明两个通用方法,例如:
public interface ChannelSettingDAO {
<T> T getSetting(IChannelSetting<T> channelSetting);
<T> void setSetting(IChannelSetting<T> channelSetting, T value);
}
我正在尝试构建一个可以处理不同设置类型的 DAO。我想知道是否有一种巧妙的方法可以做到这一点而不会出现运行时错误。
public interface ChannelSettingDAO {
Integer getIntegerSetting(ChannelSettingInteger channelSettingInteger);
String getStringSetting(ChannelSettingString channelSettingString);
Double getDoubleSetting(ChannelSettingDouble channelSettingDouble);
void setIntegerSetting(ChannelSettingInteger channelSettingInteger, Integer value);
void setStringSetting(ChannelSettingString channelSettingString, String value);
void setDoubleSetting(ChannelSettingDouble channelSettingDouble, Double value);
}
public enum ChannelSettingInteger {
CHANNEL_LOOKBACK(50);
private Integer defaultValue;
ChannelSettingInteger(Integer defaultValue) {
this.defaultValue = defaultValue;
}
public Integer getDefaultValue() {
return defaultValue;
}
}
etc.. for every type of enum.
有没有更简洁的方法来做到这一点。我觉得我错过了一些东西,可能是某种为枚举赋予类型的方法,或者我错过了一些模式。
至少有一种方法可以强制 getDefault 名称相同。
有什么建议吗?
Is there any neater way to do this. I feel like I'm missing out on something, some way to give a type to an enum maybe, or some pattern I've missed out on. At least a way to enforce that the getDefault name is the same.
你是对的,你可以使用另一种设计来更好地满足你的需求。
您可以引入一个接口,每个枚举都必须实现该接口才能拥有 getDefault()
方法。通过这种方式,您可以确保每个枚举具有相同的基类型(接口中的一个)并且自从在接口中声明后就提供了 getDefault()
。
通过在接口上使用通用类型,您允许每个枚举都有自己的 getDefaultValue
示例代码:
public interface IChannelSetting<T> {
public T getDefaultValue();
}
public enum ChannelSettingInteger implements IChannelSetting<Integer> {
CHANNEL_LOOKBACK(50);
private Integer defaultValue;
ChannelSettingInteger(Integer defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public Integer getDefaultValue() {
return defaultValue;
}
}
我不知道你将如何使用你的 DAO,但只是为了你的个人信息,如果与你的需要相关,你可以进一步利用公共基础接口来获得对称逻辑和更少的代码在你的 DAO 中。
事实上,您可以在 DAO 中只声明两个通用方法,例如:
public interface ChannelSettingDAO {
<T> T getSetting(IChannelSetting<T> channelSetting);
<T> void setSetting(IChannelSetting<T> channelSetting, T value);
}