什么是Java Bean的好用例? [英] What is a good use case of Java Beans?

查看:60
本文介绍了什么是Java Bean的好用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到了Java Beans规范.我觉得仅使用getter,setter和空构造函数会使代码更加忙碌,而且开发人员需要管理轨道以正确设置所有必需的实例.

I just saw the Java Beans specification. I felt that using only getters, setters and empty constructor will make code more hectic plus developer needs to manage track to set all the required instances properly.

有人可以为我指出一个使用这种设计的好用例吗?我感觉不到.我可以想到一种用途-当您需要创建一个类的实例,以便稍后在代码中确定其数据成员时."

Could somebody point me a good use case for using such design? I am unable to get a feel of it. There is one use I can think of - "When you need to create an Instance of a class such that its data members will be decided later in the code."

伙计们,我没有开始讨论.不想讨论优点/缺点.我不是在寻找像spring这样的当前库,因此必须使用Beans.我正在寻找一个示例来了解Java Bean将带来的工程优势. 只是一个示例,Java Beans设计将对您有所帮助"

Guys I am not initiating a discussion. Don't want to discuss advantages/disadvantages. I am not looking for current libraries like spring etc which makes it mandatory to use Beans. I am looking for an example to understand engineering benefits Java beans would bring. "Just one example where Java Beans design would help"

推荐答案

许多库和规范(例如JPA,JavaEL)都使用Java Beans规范并完全依赖该行为.

A number of libraries and specifications (e.g. JPA, JavaEL) use the Java Beans spec and rely on exactly that behavior.

使用getter和setter的优点是可以在这些方法中添加代码,例如创建虚拟"属性,这些属性是在运行时计算的,而不是存储在字段中的.

Using getters and setters has the advantage of letting you add code inside those methods, e.g. to create "virtual" properties which are calculated at runtime rather than stored in a field.

另外使用getter和setter允许其他框架包装这些方法并提供其他功能,例如更改日志记录等.在许多情况下,这是通过内部创建子类并覆盖getter和setter来完成的.用户不会注意到,因为编织"或代理"通常是在运行时完成的.例如,当您调用getter访问相关集合或实体时,Hibernate便使用此功能提供延迟加载功能.

Additionally using getters and setters allows other frameworks to wrap those methods and provide additional functionality like change logging etc. In many cases this is done by internally creating a subclass and overriding the getters and setters. The user would not notice that since that "weaving" or "proxying" is often done at runtime. Hibernate for example uses this to provide lazy loading functionality when you call a getter to access a related collection or entity.

更新:

根据请求提供虚拟"属性的示例:

As per request an example for "virtual" properties:

假设您有一个Person bean,它具有字段firstNamelastName.您可以通过提供以下getter来添加只读虚拟属性name:

Assume you have a Person bean which has the fields firstName and lastName. You could add a read-only virtual property name by providing the following getter:

public String getName() {
  return getFirstName() + " " + getLastName();
}

更新2:

关于为什么必须使用getter和setter的另一条注释:这基本上来自Java的工作方式.直接支持属性的语言(如C#)将允许您编写person.firstName = "Joe";之类的代码,如果有的话仍然使用setter,如果属性为只读,则抛出错误.因此,如果您为firstName添加一个setter,这些语言将在内部将firstName = "Joe"转换为setFirstName("Joe"),而开发人员无需更改任何内容-一种非常优雅的解决方案. :)

Another note on why getters and setters are necessary: this basically comes from how Java works. Languages that directly support properties, like C#, would allow you to write code like person.firstName = "Joe"; and still use a setter if there is one or throw an error if the property is read-only. So if you'd add a setter for firstName those languages would internally translate firstName = "Joe" to setFirstName("Joe") without the developer having to change anything - quite an elegant solution. :)

由于Java不支持,所以我们必须始终提供访问器方法(设置器/获取器),即使它们没有做任何特殊的事情-以防万一需要更改它们的 未来.

Since Java doesn't support that, we have to always provide accessor methods (setters/getters) even if they don't do anything special - just in case they might need to be changed in the future.

这篇关于什么是Java Bean的好用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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