简体   繁体   English

在从文件读取更好的数据行之前,枚举有多复杂(例如.csv)?

[英]How complex can an enum get before reading lines of data from a file is better (e.g. .csv)?

I have created part of a platformer game where I have an actor class, representing instances of objects and characters, and a type enum which denotes what data each actor will have (such as speed, attack, defence, etc). 我创建了一个平台游戏的一部分,其中有一个演员类,代表对象和角色的实例,以及一个类型枚举,表示每个演员将拥有哪些数据(例如速度,攻击,防御等)。 Specifying all the data of each type seems somewhat tedious with hardcoded enum definitions. 使用硬编码的枚举定义来指定每种类型的所有数据似乎有些乏味。 Eg 例如

PLANE("Plane", 10, 10, 2, 0),
MISSILE("Missile", 1, 0, 0, 4);

Would it be best to specify data in a file and read it instead? 最好在文件中指定数据并读取它吗?

My answer to that question would now be: Take a DSL for it, for instance using Kotlin (similar things are possible in Groovy). 我对该问题的回答现在是:为此使用DSL,例如使用Kotlin(在Groovy中可能有类似的事情)。

Without going into any detail, the following can be valid Kotlin (actually working code copied from a production system): 无需赘述,以下内容可能是有效的Kotlin(实际上是从生产系统复制的工作代码):

buildTreats {
    category(BEVERAGE) {
        treat(id = 2, key = "bonusfood.treat.appleJuice") {
            47.kcal
            0.5 of GLASS
        }
    …
    }
    …
}

The advantage is that you have full code completion and compile-time checks that you just cannot have with a CSV file. 这样做的好处是您具有完整的代码完成和编译时检查,而这些检查和检查是CSV文件无法做到的。

Some thoughts here: 这里的一些想法:

  • instead of giving a "human readable" name string to your enum, you could as well @Override the toString() method to automatically return Plane for PLANE (easy - just lowercase everything except first letter) 与其为枚举提供“易于阅读的”名称字符串,还可以@Override toString()方法自动返回Plane for PLANE(简单-仅将首字母以外的所有内容都小写)
  • such code breaks your ability to add more features later on 这样的代码破坏了您以后添加更多功能的能力

There is the old Open/Closed principle . 有一个古老的开放/封闭原则 Basically it is saying: your code needs should be open in those areas where change is most likely to happen. 基本上是这样说的:您的代码需求应该在最有可能发生更改的区域中公开

I am pretty sure: today you need 4 values, like 10, 10, 2, 0 for a Plane, as above. 我敢肯定:今天你需要4个值,比如10, 10, 2, 0为一个平面,如上述。 But in a few days, you might want to have more attributes. 但是几天后,您可能希望拥有更多属性。 Then you have to turn to all your enum constants and update them. 然后,您必须转到所有枚举常量并更新它们。

In that sense, it might be better to give up a bit of compile time safety here; 在这个意义上,它可能是更好地放弃了一下这里编译时的安全性; and use a Map instead. 并改用地图。 Map key could be an enum (just listing the different potential properties some player/item/... has). 地图键可以是一个枚举(仅列出某些玩家/物品/ ...具有的不同潜在属性)。 Map key would be the specific value for that strength. 映射键将是该强度的特定值。

If you would want to go down that route, you could then specify a type for each enum constant. 如果您想沿这条路线走,则可以为每个枚举常量指定一个类型 So that you know that a map value for STRENGTH for example would be a Integer object, or something alike. 因此,您知道例如STRENGTH的映射值将是Integer对象或类似的对象。

In other words: you might turn into the dark realms of reflection here. 换句话说:您可能会在这里变成反射的黑暗境界。

The beauty of enum is that they are fixed in code and will not change. enum的美丽之处在于它们固定在代码中并且不会更改。 This provides huge benefits when coding, type checking and you can make Map s and Set of them and you can iterate across them and even switch-case with them (although that is less common now they can have their own functionality). 这在编码,类型检查时提供了巨大的好处,您可以使它们成为MapSet ,并且可以遍历它们甚至切换大小写(尽管现在已经不那么普遍了,它们可以具有自己的功能)。 You can even save them in a database so long as you use their names and the data still maps back correctly even when the enum list changes. 您甚至可以将它们保存在数据库中,只要您使用它们的名称,即使枚举列表发生更改,数据仍然可以正确地映射回去。

As such, I would not abandon them , I would probably add a new one called perhaps "OTHERS" which can somehow provide any new ones that are experimental etc. 因此,我不会放弃它们,我可能会添加一个新的名称,也许是“ OTHERS”,它可以以某种方式提供任何实验性的新名称。

If your core issue is with the tediousness of maintaining the parameters then by all means read that data in from configuration using a static initialiser. 如果您的核心问题是繁琐的参数维护,那么请务必使用静态初始化程序从配置中读取该数据。

暂无
暂无

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

相关问题 如何从服务器读取文本文件并仅在textview中显示它的最后100行 - How to read text file from server and display only e.g. 100 last lines of it in textview 如何清除奇怪字符(例如SUB)中的csv文件? - How to clean a csv file from weird characters (e.g. SUB)? 我如何从Locale.getISOCountries()获得“ en_US”; - How can I get e.g. “en_US” from Locale.getISOCountries(); 如何获取“ Enum.CONSTANT”以返回某种类型(例如,颜色)? - How do I get “Enum.CONSTANT” to return some type (e.g. Color)? ApacheSpark从http源(例如csv等)读取数据帧 - ApacheSpark read dataframe from http source (e.g. csv, …) 如何从非常重的文件中读取字节? 然后将其存储在字符串中,例如.pdf .zip .xlsx文件 - How can i read bytes from a very heavy file? then store store them in a String e.g. .pdf .zip .xlsx files Kotlin:获取文件的扩展名,例如“.txt” - Kotlin: Get file's extension e.g. “.txt” 如何获得控制台输出(例如,在eclipse上)并将相同的输出写入/复制到文件? - how to get a console output (e.g. on eclipse) AND write/copy the same output to file? 在 Java 正则表达式中,如何获取字符类(例如 [az])以匹配 - 减号? - In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign? 如何将JPEG压缩算法用于一维数据(例如一条线)? - How can I use JPEG Compression algorithm for 1D data (e.g. a line)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM