DTO、VO、POJO、JavaBean 之间的区别?

发布于 2024-08-08 15:10:55 字数 361 浏览 6 评论 0原文

见过一些类似的问题:

您还可以告诉我它们的使用上下文吗?或者说他们的目的是什么?

Have seen some similar questions:

Can you also please tell me the contexts in which they are used? Or the purpose of them?

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

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

发布评论

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

评论(9

扛起拖把扫天下 2024-08-15 15:10:55

JavaBean

JavaBean 是一个遵循 JavaBeans 约定 的类正如 Sun 所定义的。维基百科对 JavaBean 进行了很好的总结:

JavaBean 是可重用的 Java 软件组件,可以在构建器工具中进行可视化操作。实际上,它们是用 Java 编程语言编写的符合特定约定的类。它们用于将许多对象封装到单个对象(bean)中,以便它们可以作为单个 bean 对象而不是多个单独的对象传递。 JavaBean 是可序列化的 Java 对象,具有空构造函数,并允许使用 getter 和 setter 方法访问属性。

为了充当 JavaBean 类,对象类必须遵守有关方法命名、构造和行为的某些约定。这些约定使得拥有可以使用、重用、替换和连接 JavaBean 的工具成为可能。

所需的约定是:

  • 该类必须有一个公共默认构造函数。这允许在编辑和激活框架内轻松实例化。
  • 类属性必须可以使用 get、set 和其他方法(所谓的访问器方法和修改器方法)进行访问,并遵循标准命名约定。这允许轻松地自动检查和更新框架内的 bean 状态,其中许多框架包括针对各种类型属性的自定义编辑器。
  • 该类应该是可序列化的。这使得应用程序和框架能够以独立于 VM 和平台的方式可靠地保存、存储和恢复 Bean 的状态。

由于这些要求主要以约定的形式表达,而不是通过实现接口来表达,因此一些开发人员将 JavaBean 视为遵循特定命名约定的普通旧式 Java 对象。

POJO

普通旧 Java 对象或 POJO 是一个最初引入的术语,用于指定简单的轻量级 Java 对象,不实现任何 javax.ejb 接口,与重量级 EJB 2.x(尤其是实体 Bean、无状态在我看来,会话 Bean 并没有那么糟糕)。今天,这个术语用于任何没有额外内容的简单对象。同样,维基百科在定义 POJO 方面做得很好:

POJO 是 Plain Old Java 的缩写
目的。这个名字是用来强调
该对象是
普通的Java对象,不是特殊的
对象,特别是不是
Enterprise JavaBean(尤其是之前
EJB 3)。这个词是马丁创造的
福勒、丽贝卡·帕森斯和乔什
麦肯齐,2000 年 9 月:

<块引用>

“我们想知道为什么人们如此反对在他们的生活中使用常规物体
系统并得出结论认为
因为简单的物体缺乏花哨
姓名。所以我们给了他们一个,那就是
非常受欢迎。”

该术语延续了以下模式:
技术的旧术语
不使用花哨的新功能,例如
POTS(普通老式电话服务)
电话和 PODS(普通旧数据
C++ 中定义的结构体
但仅使用 C 语言功能,并且
Perl 中的 POD(普通旧文档)。

这个词很可能已经流行了
广泛接受是因为
需要一个共同且容易的
理解与对比的术语
复杂的对象框架。一个
JavaBean 是一个 POJO,它是
可序列化,无参数
构造函数,并允许访问
使用 getter 和 setter 的属性
方法。 Enterprise JavaBean 不是
单个类但整个组件
模型(同样,EJB 3 减少了
Enterprise JavaBeans 的复杂性)。

随着使用 POJO 的设计变得
更常用的是,系统有
的出现为 POJO 提供了一些
框架中使用的功能和
对哪些领域有更多选择
实际需要的功能。
Hibernate 和 Spring 就是示例。

值对象

值对象或 VO 是一个诸如 java.lang.Integer 之类的对象,它保存值(因此是值对象)。对于更正式的定义,我经常参考 Martin Fowler 对 Value Object 的描述:

在企业应用程序架构模式中,我将值对象描述为一个小对象,例如货币或日期范围对象。它们的关键属性是它们遵循值语义而不是引用语义。

您通常可以告诉他们,因为他们的相等概念不是基于身份,而是如果两个值对象的所有字段都相等,则它们相等。尽管所有字段都相等,但如果子集是唯一的,则无需比较所有字段 - 例如货币对象的货币代码足以测试相等性。

一般的启发是值对象应该是完全不可变的。如果要更改值对象,则应该用新对象替换该对象,并且不允许更新值对象本身的值 - 可更新的值对象会导致别名问题。

早期的 J2EE 文献使用术语值对象来描述不同的概念,我称之为数据传输对象< /a>.此后,他们改变了用法,改用术语传输对象。< /p>

您可以在 wikiwiki 上找到更多关于值对象的好材料="http://www.riehle.org/computer-science/research/1998/ubilab-tr-1998-10-1.html" rel="noreferrer">德克·里尔。

数据传输对象

数据传输对象或 DTO 是 EJB 引入的一种(反)模式。这个想法不是在 EJB 上执行许多远程调用,而是将数据封装在可以通过网络传输的值对象中:数据传输对象。维基百科对数据传输对象有一个不错的定义:

数据传输对象(DTO),以前称为值对象或VO,是一种用于在软件应用程序子系统之间传输数据的设计模式。 DTO 通常与数据访问对象结合使用,以从数据库检索数据。

数据传输对象与业务对象或数据访问对象之间的区别在于,DTO 除了存储和检索其自身数据(访问器和修改器)之外没有任何行为。

在传统的 EJB 架构中,DTO 有双重用途:首先,它们解决了实体 Bean 不可序列化的问题;其次,它们解决了实体 Bean 不可序列化的问题。其次,它们隐式定义了一个组装阶段,在该阶段中,视图要使用的所有数据都将被获取并编组到 DTO 中,然后再将控制权返回到表示层。


因此,对于许多人来说,DTO 和 VO 是同一件事(但正如我们所见,Fowler 使用 VO 来表示其他含义)。大多数时候,它们遵循 JavaBean 约定,因此也是 JavaBean。而且都是 POJO。

JavaBeans

A JavaBean is a class that follows the JavaBeans conventions as defined by Sun. Wikipedia has a pretty good summary of what JavaBeans are:

JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.

In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behavior. These conventions make it possible to have tools that can use, reuse, replace, and connect JavaBeans.

The required conventions are:

  • The class must have a public default constructor. This allows easy instantiation within editing and activation frameworks.
  • The class properties must be accessible using get, set, and other methods (so-called accessor methods and mutator methods), following a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties.
  • The class should be serializable. This allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion that is independent of the VM and platform.

Because these requirements are largely expressed as conventions rather than by implementing interfaces, some developers view JavaBeans as Plain Old Java Objects that follow specific naming conventions.

POJO

A Plain Old Java Object or POJO is a term initially introduced to designate a simple lightweight Java object, not implementing any javax.ejb interface, as opposed to heavyweight EJB 2.x (especially Entity Beans, Stateless Session Beans are not that bad IMO). Today, the term is used for any simple object with no extra stuff. Again, Wikipedia does a good job at defining POJO:

POJO is an acronym for Plain Old Java
Object. The name is used to emphasize
that the object in question is an
ordinary Java Object, not a special
object, and in particular not an
Enterprise JavaBean (especially before
EJB 3). The term was coined by Martin
Fowler, Rebecca Parsons and Josh
MacKenzie in September 2000:

"We wondered why people were so against using regular objects in their
systems and concluded that it was
because simple objects lacked a fancy
name. So we gave them one, and it's
caught on very nicely."

The term continues the pattern of
older terms for technologies that do
not use fancy new features, such as
POTS (Plain Old Telephone Service) in
telephony, and PODS (Plain Old Data
Structures) that are defined in C++
but use only C language features, and
POD (Plain Old Documentation) in Perl.

The term has most likely gained
widespread acceptance because of the
need for a common and easily
understood term that contrasts with
complicated object frameworks. A
JavaBean is a POJO that is
serializable, has a no-argument
constructor, and allows access to
properties using getter and setter
methods. An Enterprise JavaBean is not
a single class but an entire component
model (again, EJB 3 reduces the
complexity of Enterprise JavaBeans).

As designs using POJOs have become
more commonly-used, systems have
arisen that give POJOs some of the
functionality used in frameworks and
more choice about which areas of
functionality are actually needed.
Hibernate and Spring are examples.

Value Object

A Value Object or VO is an object such as java.lang.Integer that hold values (hence value objects). For a more formal definition, I often refer to Martin Fowler's description of Value Object:

In Patterns of Enterprise Application Architecture I described Value Object as a small object such as a Money or date range object. Their key property is that they follow value semantics rather than reference semantics.

You can usually tell them because their notion of equality isn't based on identity, instead two value objects are equal if all their fields are equal. Although all fields are equal, you don't need to compare all fields if a subset is unique - for example currency codes for currency objects are enough to test equality.

A general heuristic is that value objects should be entirely immutable. If you want to change a value object you should replace the object with a new one and not be allowed to update the values of the value object itself - updatable value objects lead to aliasing problems.

Early J2EE literature used the term value object to describe a different notion, what I call a Data Transfer Object. They have since changed their usage and use the term Transfer Object instead.

You can find some more good material on value objects on the wiki and by Dirk Riehle.

Data Transfer Object

Data Transfer Object or DTO is a (anti) pattern introduced with EJB. Instead of performing many remote calls on EJBs, the idea was to encapsulate data in a value object that could be transfered over the network: a Data Transfer Object. Wikipedia has a decent definition of Data Transfer Object:

Data transfer object (DTO), formerly known as value objects or VO, is a design pattern used to transfer data between software application subsystems. DTOs are often used in conjunction with data access objects to retrieve data from a database.

The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behaviour except for storage and retrieval of its own data (accessors and mutators).

In a traditional EJB architecture, DTOs serve dual purposes: first, they work around the problem that entity beans are not serializable; second, they implicitly define an assembly phase where all data to be used by the view is fetched and marshalled into the DTOs before returning control to the presentation tier.


So, for many people, DTOs and VOs are the same thing (but Fowler uses VOs to mean something else as we saw). Most of time, they follow the JavaBeans conventions and are thus JavaBeans too. And all are POJOs.

夜清冷一曲。 2024-08-15 15:10:55

DTO 与 VO

DTO vs VO

???? DTO - Data Transfer Object is just a data container which is used to transport data between layers and tiers.

  • It mainly contains attributes. You can even use public attributes without getters and setters.
  • Data Transfer Objects do not contain any business logic.

Analogy:
Simple registration form with attributes username, password and email ID.

  • When this form is submitted in RegistrationServlet file you will get all the attributes from view layer to business layer where you pass the attributes to java beans and then to the DAO or the persistence layer.
  • DTO helps in transporting the attributes from view layer to business layer and finally to the persistence layer.

DTO was mainly used to get data transported across the network efficiently, it may be even from JVM to another JVM.

DTOs are often java.io.Serializable - in order to transfer data across JVM.

???? VO - Value Object [1][2] represents a fixed set of data and is similar to a Java enum. A Value Object's identity is based on their state rather than on their object identity and is immutable. A real world example would be Color.RED, Color.BLUE, SEX.FEMALE etc.


POJO vs JavaBeans

[1]
The "Java-Beanness" of a POJO is that its private attributes are all accessed via public getters and setters that conform to the JavaBeans conventions. e.g.

private String foo;
public String getFoo() {...}
public void setFoo(String foo) {...}; 

[2]
JavaBeans must implement Serializable and have a no-argument constructor, whereas in POJO does not have these restrictions.

墨落成白 2024-08-15 15:10:55

基本上,

DTO:“数据传输对象”可以在软件架构的不同层之间传输。

VO:“值对象”持有整数、货币等对象。

POJO:普通旧 Java 对象,不是特殊对象。

Java Bean:需要可序列化的 Java 类、无参数构造函数以及每个字段的 getter 和 setter

Basically,

DTO: "Data transfer objects " can travel between seperate layers in software architecture.

VO: "Value objects " hold a object such as Integer,Money etc.

POJO: Plain Old Java Object which is not a special object.

Java Beans: requires a Java Class to be serializable, have a no-arg constructor and a getter and setter for each field

雨后彩虹 2024-08-15 15:10:55

Java Bean 与 EJB 不同。

Java 1.0 中的 JavaBeans 规范 是 Sun 试图允许的在类似于 VB 的 IDE 中操作 Java 对象。对于符合“Java Beans”资格的对象制定了规则:

  1. 默认构造函数
  2. 私有数据成员的 Getter 和 setter 遵循正确的命名约定
  3. Serialized
  4. 也许还有其他我忘记的。

EJB 后来出现。它们结合了分布式组件和事务模型,在管理线程、池、生命周期并提供服务的容器中运行。它们与 Java Bean 相去甚远。

DTO 在 Java 环境中出现,因为人们发现 EJB 1.0 规范与数据库过于“啰嗦”。人们不会对每个数据元素进行往返,而是将它们批量打包成 Java Bean 并进行传送。

POJO 是针对 EJB 的反应。

Java Beans are not the same thing as EJBs.

The JavaBeans specification in Java 1.0 was Sun's attempt to allow Java objects to be manipulated in an IDE that looked like VB. There were rules laid down for objects that qualified as "Java Beans":

  1. Default constructor
  2. Getters and setters for private data members that followed the proper naming convention
  3. Serializable
  4. Maybe others that I'm forgetting.

EJBs came later. They combine distributed components and a transactional model, running in a container that manages threads, pooling, life cycle, and provides services. They are a far cry from Java Beans.

DTOs came about in the Java context because people found out that the EJB 1.0 spec was too "chatty" with the database. Rather than make a roundtrip for every data element, people would package them into Java Beans in bulk and ship them around.

POJOs were a reaction against EJBs.

决绝 2024-08-15 15:10:55

POJO
它是一个java文件(类),它不扩展或实现任何其他java文件(类)。


它是一个java文件(类),其中所有变量都是私有的,方法是公共的,并且使用适当的getter和setter来访问变量。

普通班
它是一个java文件(类),可能由公共/私有/默认/受保护变量组成,并且可能会也可能不会扩展或实现另一个java文件(类)。

POJO :
It is a java file(class) which doesn't extend or implement any other java file(class).

Bean:
It is a java file(class) in which all variables are private, methods are public and appropriate getters and setters are used for accessing variables.

Normal class:
It is a java file(class) which may consist of public/private/default/protected variables and which may or may not extend or implement another java file(class).

原来分手还会想你 2024-08-15 15:10:55
  • 值对象:当需要根据对象的值来衡量对象的相等性时使用。
  • 数据传输对象:将具有多个属性的数据一次性从客户端跨层传递到服务器,以避免对远程服务器的多次调用。
  • 普通旧 Java 对象:它就像简单的类,具有属性、公共无参数构造函数。正如我们为 JPA 实体声明的那样。

值对象模式与数据传输模式之间的差异

  • Value Object : Use when need to measure the objects' equality based on the objects' value.
  • Data Transfer Object : Pass data with multiple attributes in one shot from client to server across layer, to avoid multiple calls to remote server.
  • Plain Old Java Object : It's like simple class which properties, public no-arg constructor. As we declare for JPA entity.

difference-between-value-object-pattern-and-data-transfer-pattern

自由如风 2024-08-15 15:10:55

首先谈谈

普通类-这意味着任何在java中通常定义的类,这意味着您创建不同类型的方法属性
Bean - Bean 没什么,它只是该特定类的一个对象,使用此 Bean,您可以像对象一样访问您的 java 类。.

然后讨论最后一个 POJO

< Strong>POJO - POJO 是没有任何服务的类,它只有一个默认构造函数和私有属性以及用于设置相应的 setter 和 getter 方法的值的属性。
它是纯 Java 对象的缩写形式。

First Talk About

Normal Class - that's mean any class define that's a normally in java it's means you create different type of method properties etc.
Bean - Bean is nothing it's only a object of that particular class using this bean you can access your java class same as object..

and after that talk about last one POJO

POJO - POJO is that class which have no any services it's have only a default constructor and private property and those property for setting a value corresponding setter and getter methods.
It's short form of Plain Java Object.

迟月 2024-08-15 15:10:55

POJO(普通Java对象)

  • 是一个通常只有实例字段的类。
  • 它用于容纳数据并在功能类之间传递数据。
  • 除了 getter 和 setter 之外,它通常只有很少的方法(如果有的话)。
  • 可以被认为是超级数据类型
  • 不扩展或实现任何其他 java 类。

JavaBean 只是一个 POJO,应用了一些额外的规则。规则:

  • 它应该是可序列化的。
  • 有一个无参数构造函数。
  • 允许使用 getter 和 setter 方法访问变量(将属性标记为私有)。


PS 资源: baeldung & Udemy 以及此线程中的其他响应< /em>

POJO (plain old Java object)

  • Is a class that generally only has instance fields.
  • It's used to house data, and pass data, between functional classes.
  • It usually has few, if any methods other than getters and setters.
  • Can be thought of as a super data type.
  • Doesn't extend or implement any other java class.

A JavaBean is just a POJO, with some extra rules applied to it. Rules:

  • It should be serializable.
  • Have a no-args constructor.
  • Allow access to variables using getter and setter methods (marked properties as private).


P.S. resources: baeldung & Udemy and also other responses in this thread

吝吻 2024-08-15 15:10:55

首先,应该明确使用哪种类型以及何时使用。

DTO(数据传输对象)

数据传输对象(简称 DTO)是用于跨进程移动数据的简单对象。其主要应用涉及通过网络或应用程序的多层之间的数据传输。 DTO 通常具有私有字段以及 getter 和 setter 方法来访问它们所保存的数据。值得注意的是,DTO 充当应用程序的显示层和业务逻辑层之间信息交换的手段,但缺乏业务逻辑或验证过程。

VO(值对象)

值对象(VO)用于数据传输,与 DTO 类似。然而,与保存在数据库中或在进程之间传输的数据相反,VO 明确用于表示用户可访问的数据。在实际使用中,VO位于应用程序的显示层中。它们还可能结合进一步的技术,旨在通过数据格式化或操作来改进用户界面。

POJO(普通旧式 Java 对象):

普通旧式 Java 对象(PO​​JO)是基本的 Java 实体,没有任何独特的方法或特征。它们提供用于修改和检索数据的访问器方法和私有字段。 POJO 充当基本数据承载者,并且可以充当 DTO 或 VO。

JavaBeans:

JavaBeans 是将许多对象封装到单个对象(bean)中的类。相反,JavaBean 遵循特定的命名和访问标准。这些对象提供公共 getter 和 setter 方法来访问数据属性以及私有字段。通过 JavaBeans 确保一致且可移植的数据表示。通过各种工具和框架,它们还使序列化、反序列化和可访问性变得更容易。

有关更多详细信息,以下内容提供了清晰的概念。

  1. DTO 模式
  2. 值对象
  3. JAVA 中的 POJO
  4. JavaBeans

First of all, it should be clear which type to use and when.

DTO (Data Transfer Object)

Data transfer objects, or DTOs for short, are simple objects used to move data across processes. Its main applications involve data transmission through networks or between applications' many layers. DTOs typically have private fields as well as getter and setter methods for gaining access to the data they hold. Notably, DTOs act as a means of information interchange between the display layer and the business logic layer of an application but lack business logic or validation procedures.

VO (Value Object)

Value Objects (VOs), which are used for data transport, are similar to DTOs. However, as opposed to data that is kept in databases or transmitted between processes, VOs are expressly used to represent data that is accessible to the user. In actual use, VOs are positioned in an application's display layer. They might also incorporate further techniques designed to improve the user interface through data formatting or manipulation.

POJO (Plain Old Java Object) :

Plain Old Java Objects, or POJOs, are basic Java entities without any unique methods or characteristics. They provide accessor methods and private fields for modifying and retrieving data. POJOs serve as basic data bearers and could act as DTOs or VOs.

JavaBeans:

JavaBeans are classes that encapsulate many objects into a single object (the bean). Contrarily, JavaBeans follow particular naming and access standards. These objects offer public getter and setter methods for gaining access to data attributes as well as private fields. Consistent and portable data representation is ensured via JavaBeans. Through a variety of tools and frameworks, they also make serialization, deserialization, and accessibility easier.

For more detail information, the following contents provide clear concept.

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