术语“价值对象”的词源/含义是什么?

发布于 2024-09-27 16:40:34 字数 611 浏览 8 评论 0原文

我是一名具有.NET / PHP背景的程序员。

我最近回顾了关于 Flashbuilder 4 / ActionScript 的视频培训。视频培训中的一个视频名为“使用值对象创建数据模型”。我提到“值对象”对我来说是一个陌生的术语,并且真的不知道他是否指的是“模型”,这有点令人困惑,因为我只能将其理解为“模型”,但标题表明我们正在创建一个带有值对象的模型,表明它们是两个不同的实体。

培训师说,他被告知这是 Java 中一个相当常见的术语,有时也称为“传输对象”。事实上,“值对象”的维基百科页面重定向到“数据传输对象”,以及 stackoverflow 问题,例如 这个提到“值对象”也可能是“Java 中的映射对象”。

该术语的词源是什么?它与其他数据模型相关术语有何关系?它是否来自 Java 世界并在某个时候不再广泛使用?

I am a programmer with a .NET / PHP background.

I recently reviewed a video training on Flashbuilder 4 / ActionScript. One of the videos in the video training is named "Creating a Data Model with a Value Object". I mentioned that "value object" was an unfamiliar term to me and didn't really know if he meant by it "model" or not and it was a bit confusing since I can only understand it as a "model" yet the title suggests that we are creating a model with a value object, suggesting they are two different entities.

The trainer said that he was told this was a fairly common term in Java, also sometimes referred to as a "transfer object". In fact, the Wikipedia page for "value object" redirects to "Data transfer object", and stackoverflow questions like this one mention that a "value object" might also be a "mapped object in Java".

What is the etymology of this term and how does it relate to other data-model-related terms? Does it come from the Java world and did it at some point go out of wide-spread use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

红ご颜醉 2024-10-04 16:40:34

这个术语似乎在不同的上下文中被过度使用。 Misko Hevery 将该术语描述为:我的解释是:

Injectable 类可以在其构造函数中请求其他 Injectable。 (有时我将 Injectables 称为服务对象,但该术语已被重载。) [...] 以下是我希望从 DI 框架中获得的一些类示例:CreditCardProcessor、MusicPlayer、MailSender ,离线队列。

类似地,Newables 可以在其构造函数中请求其他 Newables,但不能请求 Injectables(有时我将 Newables 称为值对象,但同样,该术语已被重载)。 Newables 的一些示例包括:电子邮件、MailMessage、用户、信用卡、歌曲。如果您保留这种区别,您的代码将易于测试和使用。如果您违反此规则,您的代码将很难测试。


我同意的另一个描述是 http://c2.com/cgi/wiki?ValueObject< /a>

值对象的示例有数字、日期、货币和字符串。通常,它们是用途广泛的小物体。他们的身份基于他们的状态而不是他们的对象身份。这样,您就可以拥有同一概念值对象的多个副本。

因此,我可以拥有代表日期 1998 年 1 月 16 日的对象的多个副本。这些副本中的任何一个都将彼此相等。对于像这样的小对象,创建新对象并移动它们通常比依赖单个对象来表示日期更容易。

值对象应始终覆盖 Java 中的 .equals()(或 Smalltalk 中的 =)。 (记住也要覆盖 .hashCode()。)

The term seems to be overloaded in different contexts. Misko Hevery describes the term as I interpret it:

An Injectable class can ask for other Injectables in its constructor. (Sometimes I refer to Injectables as Service Objects, but that term is overloaded.) [...] Here are some examples of classes I would expect to get from my DI framework: CreditCardProcessor, MusicPlayer, MailSender, OfflineQueue.

Similarly Newables can ask for other Newables in their constructor, but not for Injectables (Sometimes I refer to Newables as Value Object, but again, the term is overloaded). Some examples of Newables are: Email, MailMessage, User, CreditCard, Song. If you keep this distinctions your code will be easy to test and work with. If you break this rule your code will be hard to test.

Another description that I would agree with is the one at http://c2.com/cgi/wiki?ValueObject

Examples of value objects are things like numbers, dates, monies and strings. Usually, they are small objects which are used quite widely. Their identity is based on their state rather than on their object identity. This way, you can have multiple copies of the same conceptual value object.

So I can have multiple copies of an object that represents the date 16 Jan 1998. Any of these copies will be equal to each other. For a small object such as this, it is often easier to create new ones and move them around rather than rely on a single object to represent the date.

A value object should always override .equals() in Java (or = in Smalltalk). (Remember to override .hashCode() as well.)

追我者格杀勿论 2024-10-04 16:40:34

我在 Jazoon 2010 上,Kevlin Henney 发表了关于“对象的价值”的演讲 - 我强烈推荐 观看阅读演示文稿(涵盖价值对象)。对于我来说,内容太多,无法轻松总结,但我的解释是:

值对象

表示模型中信息的对象。它没有身份并且是不可变的。例如,日期 2010 年 10 月 13 日可以表示为值对象。它没有身份,2010 年 10 月 13 日始终是 2010 年 10 月 13 日 - 它永远不会改变。

如果有机会的话,我还建议您亲自聆听凯夫林的演讲。我发现他是一位非常好的演讲者。

您可能还想查看 Eric Evan 的《领域驱动设计》一书,因为我相信这是 Java 世界中值对象概念的现代来源。

I was at Jazoon 2010 where Kevlin Henney gave a talk on 'the Value of Objects' - I highly recommend watching or reading the presentation (which covers value objects). There's too much content for me to easily summerise, but my interpretation would be:

Value Object

An object representing information in a model. It does not have identity and is immutable. For example, the date 13 October 2010 can be represented as a value object. It does not have an identity and the 13 October 2010 is always the 13 October 2010 - it never changes.

I also recommend taking the opportunity to listen to Kevlin in person if you get the chance. I find him to be damn good speaker.

You may also want to check out Eric Evan's Domain Driven Design book as I believe this is the modern-day source of the concept of value objects in the Java world.

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