java reflection 替代,JavaBeans替代品?

博主表达了对JavaBean模式繁琐性的不满,并提出了一个替代方案,通过创建一个名为`Property`的类来实现单行代码声明属性,并内置属性变更支持。这个新设计使得在2009年的代码中,不必再为每个属性编写大量的代码。尽管这仍需要一些反射操作,但已显著减少了代码量。此外,还提到了可以进一步优化的地方,如处理序列化、空值检查等。
摘要由CSDN通过智能技术生成

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

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 name = new Property();

}

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

Help me off the javabean train!

解决方案

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 {

public final String name;

T value;

private final PropertyChangeSupport support;

public static Property newInstance(String name, T value,

PropertyChangeSupport support) {

return new Property(name, value, support);

}

public static Property 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(); }

}

and then go ahead and use it:

public class Customer {

private final PropertyChangeSupport support = new PropertyChangeSupport();

public final Property name = Property.newInstance("name", "", support);

public final Property 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);

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:

Properly handle serialization

Handle null value checking

Maybe add a specializations for atomic types if you care about autoboxing overhead.

?? I'm sure there are more gotchas

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值