EnumMap 是否可以被视为 Java bean 的合理替代方案?
好奇是否有人考虑使用 EnumMap 代替 Java beans,特别是“值对象”(没有行为)?对我来说,似乎一个优点是“属性”的名称可以从支持枚举直接访问,不需要反射,因此我认为它会更快。
Curious if anybody has considered using EnumMap in place of Java beans, particularly "value objects" (with no behavior)? To me it seems that one advantage would be that the name of a "property" would be directly accessible from the backing Enum, with no need for reflection, and therefore I'd assume it would be faster.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它可能比使用反射快一点(我没有测量它,也没有在Google中找到任何指标);然而这种方法有很大的缺点:
你正在失去类型安全。一切都是
Object get(MyEnum.FIELD_NAME)
,而不是int getAge()
和String getName()
。这将导致一些丑陋的代码和运行时错误。我们喜爱和享受的所有 javabean 细节(例如,属性级注释)都消失了。
由于您可以完全没有行为,因此该方法的适用性似乎相当有限。
由于您可以完全
底线是 - 如果您确实需要所谓的:-)性能提升(您必须衡量以证明它存在),这在非常特定的情况下可能是一种可行的方法。它是整个 javabean 的可行替代品吗?肯定不是。
It may be a little faster then using reflection (I didn't measure it, didn't find any metrics in Google either); however there are big disadvantages to this approach:
You're losing type safety. Instead of
int getAge()
andString getName()
everything isObject get(MyEnum.FIELD_NAME)
. That'll provide for some ugly code and run-time errors right there.All the javabean niceties we've come to love and enjoy (for example, property-level annotations) are gone.
Since you can have NO BEHAVIOR AT ALL, the applicability of this approach seems rather limited.
The bottom line is - if you really truly need that alleged :-) boost in performance (which you'll have to measure to prove it exists) this may be a viable approach under very specific circumstances. Is it a viable alternative to javabeans at large? Most certainly not.
bean 应该是可变的,因此有 setter 方法。 EnumMap 在速度上与使用以整数作为键的 HashMap 相当,但键是不可变的。 Bean 和 EnumMap 有两个不同的用途。如果所有的键在设计时都是已知的,并且保证永远不会改变,那么使用 EnumMap 就可以了。
更新 bean 比更改 EnumMap 的支持 Enum 简单得多,并且在代码下游创建错误的可能性要小得多。
A bean is meant to be mutable, hence the setter methods. EnumMap is comparable in speed to using a HashMap with integers as the Key, but are Keys are Immutable. Beans and EnumMaps serve two different purposes. If all of the Keys are known at design time and are guaranteed to never change, then using an EnumMap will be fine.
Updating a bean is much simpler than changing the backing Enum of the EnumMap with much less chance of creating errors downstream in the code.
我编写了一个 Record 类,它将键映射到值,并通过委托给完全同步的 EnumMap 来工作。这个想法是 Record 可以在运行时获取新字段,而 Bean 则不能。我的结论是,这种灵活性会带来性能损失。下面是将 Record 类与完全同步的 Bean 进行比较的运行。对于 1000 万次操作:
因此,了解数据对象并编写静态建模类是有好处的。如果您想在运行时将新字段填充到数据中,则需要付出代价。
I wrote a Record class that maps keys to values and works by delegating to a fully synchronized EnumMap. The idea is that a Record can get new fields at runtime whereas the Bean can't. My conclusion is that with this flexibility comes a performance hit. Here's a run comparing the Record class to a fully synchronized Bean. For 10 million operations:
So, there is something to gain in knowing your data objects and writing a class that models them statically. If you want to have new fields padded on to your data at runtime, it will cost you.
我不明白如何使用 EnumMaps 删除“类分析”。除非您有一个具有 20 多个属性的通用枚举可供每个“bean”重用,否则您仍然需要发明一个用于每个枚举映射的枚举,例如,
而不是更不用说
现在一切都是字符串。
I don't understand how you can remove 'class profileration' with EnumMaps. Unless you have a generic enum with 20-odd properties to reuse for every 'bean', you're still inventing an enum to use for each enum map, e.g.
as opposed to
Not to mention that everything is a String now.
我之前没有指定这一点,但我正在使用结果集。因此,为了完整起见,我想提供这个答案。
Commons/BeanUtil 的 "RowSetDynaClass" 可能是与具体 bean 相关的过多样板和 EnumMap 的限制之间的折衷方案
I had not previously specified this, but I am working with a ResultSet. Therefore I want to provide this answer for the sake of completeness.
Commons/BeanUtil's "RowSetDynaClass" could be the happy medium between the excessive boilerplate associated with concrete beans, and the limitations of EnumMap