EnumMap 是否可以被视为 Java bean 的合理替代方案?

发布于 2024-08-03 22:05:38 字数 102 浏览 5 评论 0原文

好奇是否有人考虑使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无法言说的痛 2024-08-10 22:05:38

可能比使用反射快一点(我没有测量它,也没有在Google中找到任何指标);然而这种方法有很大的缺点:

  1. 你正在失去类型安全。一切都是 Object get(MyEnum.FIELD_NAME),而不是 int getAge()String getName()。这将导致一些丑陋的代码和运行时错误。

  2. 我们喜爱和享受的所有 javabean 细节(例如,属性级注释)都消失了。

  3. 由于您可以完全没有行为,因此该方法的适用性似乎相当有限。

    由于您可以完全

底线是 - 如果您确实需要所谓的:-)性能提升(您必须衡量以证明它存在),这在非常特定的情况下可能是一种可行的方法。它是整个 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:

  1. You're losing type safety. Instead of int getAge() and String getName() everything is Object get(MyEnum.FIELD_NAME). That'll provide for some ugly code and run-time errors right there.

  2. All the javabean niceties we've come to love and enjoy (for example, property-level annotations) are gone.

  3. 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.

圈圈圆圆圈圈 2024-08-10 22:05:38

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.

新人笑 2024-08-10 22:05:38

我编写了一个 Record 类,它将键映射到值,并通过委托给完全同步的 EnumMap 来工作。这个想法是 Record 可以在运行时获取新字段,而 Bean 则不能。我的结论是,这种灵活性会带来性能损失。下面是将 Record 类与完全同步的 Bean 进行比较的运行。对于 1000 万次操作:

Record  set(Thing, a)  458 ms
Bean    setThing(a)    278 ms
Record  get(Thing)     398 ms
Bean    getThing       248 ms

因此,了解数据对象并编写静态建模类是有好处的。如果您想在运行时将新字段填充到数据中,则需要付出代价。

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:

Record  set(Thing, a)  458 ms
Bean    setThing(a)    278 ms
Record  get(Thing)     398 ms
Bean    getThing       248 ms

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.

且行且努力 2024-08-10 22:05:38

我不明白如何使用 EnumMaps 删除“类分析”。除非您有一个具有 20 多个属性的通用枚举可供每个“bean”重用,否则您仍然需要发明一个用于每个枚举映射的枚举,例如,

public enum PersonDTOEnum {
     A, S, L;
}

而不是更不用说

class Person {

    int a;
    int s;
    String l;

    // getters + setters elided
}

现在一切都是字符串。

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.

public enum PersonDTOEnum {
     A, S, L;
}

as opposed to

class Person {

    int a;
    int s;
    String l;

    // getters + setters elided
}

Not to mention that everything is a String now.

予囚 2024-08-10 22:05:38

我之前没有指定这一点,但我正在使用结果集。因此,为了完整起见,我想提供这个答案。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文