我有这个系统,可以让我为每个玩家创建和实现类型安全的变量。每个变量的定义如下:
@Foo
public static final Bar KILLS = new Bar();
注释将标记要在运行时拾取的变量,以进行注册。这些变量本质上是静态的mutator方法,可以像这样调整所讨论玩家的基础值
KILLS.set(player, 10);
该系统运行良好,但是每种类型(对象,整数,字符串等)都具有可变类型的支持,该可变类型被延迟加载到相应玩家的变量映射中。我对这种设计可能会随着玩家人数的增加而带来的潜在垃圾问题感到好奇。我知道由于Java的自动装箱机制,某些事情是不可避免的,但是其他地方可能还有一些改进的余地。我对Java内存模型并不完全熟悉,因此请原谅我的解释或缺乏它。
编辑:
为了更加清楚,每种变量类型都扩展了父类并分别提供了T类型。然后,这些子类将被访问以覆盖必需的方法,这些方法允许它们改变有关玩家变量的值。
Firstly, I would like to say I am a bit confused by your mention of "garbage issues", because your context is not entirely clear whether you're referring to Java's implicit garbage collection of runtime memory or whether you're concerned about this registration data is piling up somewhere else, like a database.
If you're referring to Java's runtime memory management: Java is (as you probably know) automatically garbage collected but you can suggest (with no guarantee whatsoever) Java to collect garbage at any time with System.gc()
. This is almost always a micro-optimization and provides little to no real benefit as Java is already amazing at determining the correct time to do garbage collection without you. I wouldn't even bother worrying about it in Java.
如果您指的是这种数据存储的外部模型,我会很担心。由于缺乏更好的术语,采用抽象的“ setter”方法可以使错误或副作用暴露在内存中。这需要在Java实现上进行双重管理,以使其尽可能避免出现错误,但是您还需要自己管理外部存储中的内存。
尽管如此,几乎可以肯定,这种情况表明支持代码需要重组和更多指导。我认为,这种抽象设置方法是一种代码气味。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句