Java豆:我错过了什么? [英] Java Beans: What am I missing?

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

问题描述

我想知道我是否遗漏了一些关于Java Bean的东西。我喜欢我的对象在构造函数中尽可能多地进行初始化并且具有最少数量的mutator。豆似乎直接反对这一点,并且通常感到笨重。我没有将我的对象构建为Beans而错过了哪些功能?

I'm wondering if I'm missing something about Java Beans. I like my objects to do as much initialization in the constructor as possible and have a minimum number of mutators. Beans seem to go directly against this and generally feel clunky. What capabilities am I missing out on by not building my objects as Beans?

推荐答案

听起来你是在正确的轨道上。并不是你错过了Java Beans的观点,而是其他程序员滥用它们。

It sounds like you are on the right track. It's not you who's missing the point of Java Beans, it is other programmers that are misusing them.

Java Beans规范旨在与可视化工具一起使用。我们的想法是,应用程序设计人员能够以交互方式配置对象的实例,然后对配置的bean进行序列化(或生成代码),以便可以在运行时重建它;意图是它不会在运行时发生变异。

The Java Beans specification was designed to be used with visual tools. The idea was that an application designer would be able to configure an instance of an object interactively, then serialize (or generate code for) the configured bean, so that it could be reconstructed at runtime; the intent was that it would not be mutated at runtime.

不幸的是,很多开发人员都不明白访问器违反封装。他们使用结构而不是对象。他们没有看到其他类,甚至其他包有任何错误,依赖于类的数据成员。

Unfortunately, a lot of developers don't understand that accessors violate encapsulation. They use structs instead of objects. They don't see anything wrong with other classes, even other packages, having dependencies on a class's data members.

当然,你通常需要配置对象的实例。只是应该通过某种配置功能来完成。这可能是依赖注入容器,BeanBox样式可视化工具,或者只是读取您手动编写的JSON,XML或属性文件。关键是在运行时这些对象实际上是不可变的;客户只是调用他们的操作,他们不访问他们的属性。

Of course, you will in general have the need to configure instances of your objects. It's just that this should be done through some sort of configuration feature. This might be a dependency injection container, a "BeanBox" style visual tool, or simply reading JSON, XML, or properties files that you wrote by hand. The key is that at runtime these objects are effectively immutable; clients just invoke their operations, they don't access their properties.

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

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