简体   繁体   中英

Java - Same Enum for different Types for DAO

I'm trying to build a DAO which will handle different setting types. I was wondering if there is a neat way to do this with no chance of runtime errors.

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.

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.

Any tips?

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.

You are right, you can use another design to better address your need.
you could introduce an interface that each enum will have to implement to have the getDefault() method. In this way, you are sure that each enum has the same base type (which one of the interface) and that getDefault() is provided since declared in the interface. By using a generic type on the interface, you allow each enum to have its own data type for getDefaultValue

Sample code :

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;
    }
}

I don't know how you will use your DAO, but just for your personal information, if relevant for your need, you could go further by taking advantage of the common base interface to have a symmetric logic and less code in your DAO.

Indeed, you could declare just two generic methods in your DAO such as :

public interface ChannelSettingDAO {
    <T> T getSetting(IChannelSetting<T> channelSetting);
    <T> void setSetting(IChannelSetting<T> channelSetting, T value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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