简体   繁体   English

每个统计信息的Enum-getter与Getter

[英]Enum-getter vs. Getter for each stat

I am currently processing a huge data input, including a lot of values, which I want to receive in getters for later use. 我目前正在处理海量数据输入,其中包括很多值,我希望在getter中接收这些值以供以后使用。

After writing a few methodes, I wondered if it might be a better idea to just use one get Method, with an enum-class containing all possible values, eg 在写了一些方法之后,我想知道是否最好只使用一个get方法,它带有一个包含所有可能值的枚举类,例如

public double getStat(StatType st) {
    if(st != null)
        return st.getValue();
}

instead of 代替

public double getAvgReflection() {
    return ...
}

public double getMaxLifesteal() {
    return ...
}

Is there any convention for using either of the two possibilities? 是否有使用两种可能性中的任何一种的约定? Any dis/advantages? 有什么缺点吗?

Thanks in advance! 提前致谢!

Using an Enum maxes it easier to add new stats, just add a constant in the Enum. 使用枚举可最大化添加新统计信息的难度,只需在枚举中添加常量即可。 But all stats need to behave the same way, ie be doubles in your example. 但是所有统计信息都必须以相同的方式运行,即在您的示例中为双精度。 What happens if you want to add an int stat? 如果您想添加一个int stat,会发生什么?

The "convention" you are asking about really boils down to the use and definition of your values. 您所要求的“惯例”实际上归结为价值观的使用和定义。 When you make the values Enums, then you must handle them as that type . 当您将值设为 Enums时,必须将其作为该类型进行处理。 Meaning, the fields in your class would have to be defined as such: 意思是,您的类中的字段必须这样定义:

private MyEnum avgReflection;
private MyEnum maxLifesteal;
...
public MyEnum getAvgReflection {
    return this.avgReflection;
}

And so forth. 依此类推。

Now, you could have your Enums return double values, but these values are static. 现在,您可以让Enums返回double值,但是这些值是静态的。 I don't think you are concerned about static values, but, instead, perhaps a static set of values. 我不认为您担心静态值,而是静态值集。

You then have two possible options: declare all possible parameters as fields, or create one aggregate field to hold all values and then use an Enum as an index: 然后,您有两个可能的选择:将所有可能的参数声明为字段,或者创建一个聚合字段来保存所有值,然后使用Enum作为索引:

public enum MyEnum {
    averageReflection(0),
    maximumLifeSteal(1);
    private int value;
    private MyEnum(int value) {
        this.value = value;
    }
    public int getValue() {
        return this.value;
    }
}
...
private double[] attributes = new double[100]; // arbitrary initialization
public double getAttribute(MyEnum attribute) {
    return this.attributes[attribute.getValue()];
}

The two restrictions on using an array (assuming you want primitive values and you are concerned about performance) is that all the values must be the same type, and the number of attributes will be set at compile time. 使用数组的两个限制(假设您需要原始值并且您担心性能)是所有值都必须是同一类型,并且属性数量将在编译时设置。

Additionally, you may just want to use a Map<String,double> or Map<MyEnum,double>, or even Map<MyEnum, Object>. 此外,您可能只想使用Map <String,double>或Map <MyEnum,double>,甚至Map <MyEnum,Object>。 A Map type will give you the ability to maintain a dynamically-sized set and possibly holding multiple types as well (with the costly overhead of converting your values). 映射类型将使您能够维护动态大小的集合,并可能还包含多个类型(转换值的开销很大)。

You should base your decision on the amount of attributes you need to keep, the kind of overhead you are willing to tolerate, and your style. 您应该根据需要保留的属性数量,您愿意忍受的开销类型以及样式来做出决定。

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

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