gpt4 book ai didi

java - 术语 "value object"的词源/含义是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:02 26 4
gpt4 key购买 nike

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

我最近查看了有关 Flashbuilder 4/ActionScript 的视频培训。视频培训中的一个视频名为“使用值对象创建数据模型”。我提到“value object”对我来说是一个陌生的术语,我真的不知道他说的是不是“model”,这有点让人困惑,因为我只能将其理解为“模型”,但标题暗示我们正在创建一个模型一个值对象,暗示它们是两个不同的实体。

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

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

最佳答案

这个词似乎在不同的上下文中被重载了。 Misko Hevery describes我解释的术语:

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.

我同意的另一个描述是 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.)

关于java - 术语 "value object"的词源/含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3921200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com