JavaBeans替代品? [英] JavaBeans alternatives?

查看:87
本文介绍了JavaBeans替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌JavaBeans模式,因为它的热情像一千个太阳的火焰一样燃烧.为什么?

I hate the JavaBeans pattern with a passion that burns like the fire of a thousand suns. Why?

  • 详细.现在是2009年.我不必为某个属性编写7 LOC.如果他们有事件监听器,那么请戴上你的帽子.
  • 没有类型安全的引用.没有类型安全的方法来引用属性. Java的全部要点是它是类型安全的,并且它最流行的模式根本不是类型安全的.
  • Verbose. It's 2009. I shouldn't have to write 7 LOC for a property. If they have event listeners then hold on to your hat.
  • No type-safe references. There is no type-safe way to reference a property. The whole point of Java is that it is type safe, and its most popular pattern is not at all typesafe.

我想要的是这样的东西:

What I would like is something like:

class Customer {
    public Property<String> name = new Property();
}

我主要是一名Web开发人员,因此需要JPA和Wicket支持.

I am a web developer mostly, so it needs JPA and Wicket support.

帮我摆脱Javabean的火车!

Help me off the javabean train!

推荐答案

我认为您已经很接近那里的声明了(请参见下面的草图).但是,通过使用非bean方法,您可能会失去大多数假定JavaBeans协议有效的工具所提供的支持.请客气.下面的代码不在我的脑海中.

I think you're pretty close with the declaration you have there (see below for a sketch). However, by using a non-beans approach, you'll probably lose support provided by most tools that assume the JavaBeans protocol is in effect. Please be kind. The code below is off the top of my head...

public class Property<T> {
    public final String name;
    T value;
    private final PropertyChangeSupport support;

    public static <T> Property<T> newInstance(String name, T value, 
                                              PropertyChangeSupport support) {
        return new Property<T>(name, value, support);
    }

    public static <T> Property<T> newInstance(String name, T value) {
        return newInstance(name, value, null);
    }

    public Property(String name, T value, PropertyChangeSupport support) {
        this.name = name;
        this.value = value;
        this.support = support;
    }

    public T getValue() { return value; }

    public void setValue(T value) {
        T old = this.value;
        this.value = value;
        if(support != null)
            support.firePropertyChange(name, old, this.value);
    }

    public String toString() { return value.toString(); }
}

然后继续使用它:

public class Customer {
    private final PropertyChangeSupport support = new PropertyChangeSupport();

    public final Property<String> name = Property.newInstance("name", "", support);
    public final Property<Integer> age = Property.newInstance("age", 0, support);

    ... declare add/remove listenener ...
}


Customer c = new Customer();
c.name.setValue("Hyrum");
c.age.setValue(49);
System.out.println("%s : %s", c.name, c.age);

因此,现在声明一个属性是一行代码,并且包含属性更改支持.我调用了方法setValue()和getValue(),所以它仍然看起来像Bean,像Rhino之类的东西进行编码,但是为了简洁起见,您可以只添加get()和set().剩下的作为练习留给读者:

So, now declaring a property is a single line of code and property change support is included. I called the methods setValue() and getValue() so it would still look like a bean to code like Rhino and stuff, but for succinctness, you could add just get() and set(). The rest is left as an exercise for the reader:

  • 正确处理序列化
  • 处理空值检查
  • 如果您关心自动装箱开销,也许可以为原子类型添加特殊化.
  • ??我确定还有更多陷阱

还请注意,您可以子类化(通常作为匿名类)并重写setValue()以提供其他参数检查.

Also note that you can subclass (usually as an anonymous class) and override setValue() to provide additional parameter checking.

我认为您真的无法摆脱字符串引用",因为这几乎就是反射的全部内容.

I don't think you can really get away from "String references" since that's pretty much what reflection's all about.

可惜的是,在当今时代,这仍然有点像在汇编中编程... Groovy,C#等,如果您有选择的话,可能仍然是更好的选择.

Sadly though, in this day and age, this is still kind of like programming in assembly... Groovy, C#, etc, etc may still be a better choice, if you have a choice.

这篇关于JavaBeans替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆