DTO、VO、POJO、JavaBeans 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1612334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between DTO, VO, POJO, JavaBeans?
提问by jai
Have seen some similar questions:
看过一些类似的问题:
- What is the difference between a JavaBean and a POJO?
- What is the Difference Between POJO (Plain Old Java Object) and DTO (Data Transfer Object)?
Can you also please tell me the contexts in which they are used? Or the purpose of them?
你能告诉我它们的使用环境吗?还是他们的目的?
采纳答案by Pascal Thivent
JavaBeans
JavaBeans
A JavaBean is a class that follows the JavaBeans conventionsas defined by Sun. Wikipedia has a pretty good summary of what JavaBeansare:
JavaBean 是遵循Sun 定义的 JavaBeans 约定的类。维基百科对JavaBeans有一个很好的总结:
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.
JavaBeans 是可重用的 Java 软件组件,可以在构建器工具中进行可视化操作。实际上,它们是用符合特定约定的 Java 编程语言编写的类。它们用于将许多对象封装到单个对象(bean)中,以便它们可以作为单个 bean 对象而不是多个单独的对象传递。JavaBean 是可序列化的 Java 对象,具有空构造函数,并允许使用 getter 和 setter 方法访问属性。
为了充当 JavaBean 类,对象类必须遵守有关方法命名、构造和行为的某些约定。这些约定使得拥有可以使用、重用、替换和连接 JavaBean 的工具成为可能。
所需的约定是:
- 该类必须具有公共默认构造函数。这允许在编辑和激活框架内轻松实例化。
- 必须遵循标准命名约定,使用 get、set 和其他方法(所谓的访问器方法和修改器方法)可以访问类属性。这允许在框架内轻松自动检查和更新 bean 状态,其中许多包括用于各种类型属性的自定义编辑器。
- 该类应该是可序列化的。这允许应用程序和框架以独立于 VM 和平台的方式可靠地保存、存储和恢复 bean 的状态。
因为这些需求主要是通过约定而不是通过实现接口来表达的,所以一些开发人员将 JavaBean 视为遵循特定命名约定的普通旧 Java 对象。
POJO
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:
一个普通的旧 Java 对象或 POJO 是一个最初引入的术语,用于指定一个简单的轻量级 Java 对象,不实现任何javax.ejb
接口,与重量级 EJB 2.x 相对(尤其是实体 Bean,无状态会话 Bean 并不是那么糟糕的 IMO)。今天,该术语用于任何没有额外内容的简单对象。同样,维基百科在定义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.
POJO 是Plain Old Java Object 的首字母缩写词。该名称用于强调所讨论的对象是普通 Java 对象,而不是特殊对象,尤其不是 Enterprise JavaBean(尤其是在 EJB 3 之前)。该术语由 Martin Fowler、Rebecca Parsons 和 Josh MacKenzie 在 2000 年 9 月创造:
“我们想知道为什么人们如此反对在他们的系统中使用常规对象,并得出结论,这是因为简单的对象缺乏一个花哨的名字。所以我们给了他们一个,而且它很受欢迎。”
该术语延续了不使用新功能的技术的旧术语模式,例如电话中的 POTS(Plain Old Telephone Service)和用 C++ 定义但仅使用 C 语言功能的 PODS(Plain Old Data Structures),和 Perl 中的 POD(Plain Old Documentation)。
该术语很可能已获得广泛接受,因为需要一个通用且易于理解的术语,与复杂的对象框架形成对比。JavaBean 是可序列化的 POJO,具有无参数构造函数,并允许使用 getter 和 setter 方法访问属性。Enterprise JavaBean 不是单个类,而是一个完整的组件模型(同样,EJB 3 降低了 Enterprise JavaBeans 的复杂性)。
随着使用 POJO 的设计变得越来越普遍,出现了一些系统,这些系统为 POJO 提供了框架中使用的一些功能,并提供了更多关于实际需要哪些功能领域的选择。Hibernate 和 Spring 就是例子。
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:
值对象或 VO 是一个对象,例如java.lang.Integer
保存值(因此是值对象)。对于更正式的定义,我经常参考 Martin Fowler 对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 Objectinstead.
You can find some more good material on value objects on the wikiand by Dirk Riehle.
在企业应用程序架构模式中,我将值对象描述为一个小对象,例如货币或日期范围对象。它们的关键特性是它们遵循值语义而不是引用语义。
您通常可以告诉他们,因为他们的相等概念不是基于身份,而是如果两个值对象的所有字段都相等,则它们相等。尽管所有字段都相等,但如果子集是唯一的,则不需要比较所有字段 - 例如,货币对象的货币代码足以测试相等性。
一般的启发式是值对象应该是完全不可变的。如果你想改变一个值对象,你应该用一个新的对象替换这个对象,并且不允许更新值对象本身的值——可更新的值对象会导致别名问题。
早期的 J2EE 文献使用术语值对象来描述一个不同的概念,我称之为数据传输对象。从那以后,他们改变了用法并改用术语传输对象。
您可以在wiki和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:
数据传输对象或 DTO 是 EJB 引入的(反)模式。这个想法不是在 EJB 上执行许多远程调用,而是将数据封装在一个可以通过网络传输的值对象中:数据传输对象。维基百科对数据传输对象有一个不错的定义:
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.
数据传输对象 (DTO),以前称为值对象或 VO,是一种用于在软件应用程序子系统之间传输数据的设计模式。DTO 通常与数据访问对象结合使用以从数据库中检索数据。
数据传输对象与业务对象或数据访问对象之间的区别在于,DTO 除了存储和检索其自身数据(访问器和修改器)之外没有任何行为。
在传统的 EJB 架构中,DTO 有双重目的:首先,它们解决了实体 bean 不可序列化的问题;其次,它们隐含地定义了一个组装阶段,在这个阶段,视图将使用的所有数据在将控制权返回给表示层之前被提取并编组到 DTO 中。
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.
所以,对于很多人来说,DTO 和 VO 是一回事(但 Fowler 使用 VO 来表示我们看到的其他含义)。大多数情况下,它们遵循 JavaBeans 约定,因此也是 JavaBeans。而且都是POJO。
回答by duffymo
Java Beans are not the same thing as EJBs.
Java Bean 与 EJB 不同。
The JavaBeans specificationin 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":
Java 1.0 中的JavaBeans 规范是Sun 试图允许在一个类似于VB 的IDE 中操作Java 对象的尝试。为符合“Java Beans”资格的对象制定了规则:
- Default constructor
- Getters and setters for private data members that followed the proper naming convention
- Serializable
- Maybe others that I'm forgetting.
- 默认构造函数
- 遵循正确命名约定的私有数据成员的 getter 和 setter
- 可序列化
- 也许其他我忘记了。
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.
EJB 是后来出现的。它们结合了分布式组件和事务模型,运行在管理线程、池化、生命周期和提供服务的容器中。它们与 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.
DTO 出现在 Java 上下文中,因为人们发现 EJB 1.0 规范与数据库过于“健谈”。人们不会对每个数据元素进行往返,而是将它们批量打包到 Java Bean 中并运送它们。
POJOs were a reaction against EJBs.
POJO 是对 EJB 的一种反应。
回答by Srinivas M.V.
DTO vs VO
DTO 与 VO
DTO -Data transfer objects are just data containers which are used to transport data between layers and tiers.
DTO -数据传输对象只是用于在层和层之间传输数据的数据容器。
- It mainly contains attributes. You can even use public attributes without getters and setters.
- Data transfer objects do not contain any business logic.
- 它主要包含属性。您甚至可以使用没有 getter 和 setter 的公共属性。
- 数据传输对象不包含任何业务逻辑。
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's helps in transporting the attributes from view layer to business layer and finally to the persistence layer.
类比:
具有用户名、密码和电子邮件 ID 属性的简单注册表。
- 当这个表单在 RegistrationServlet 文件中提交时,您将获得从视图层到业务层的所有属性,在那里您将属性传递给 java bean,然后传递给 DAO 或持久层。
- DTO 有助于将属性从视图层传输到业务层,最后传输到持久层。
DTO was mainly used to get data transported across the network efficiently, it may be even from JVM to another JVM.
DTO 主要用于有效地通过网络传输数据,它甚至可以从 JVM 到另一个 JVM。
DTOs are often java.io.Serializable
- in order to transfer data across JVM.
DTO 通常是java.io.Serializable
- 为了跨 JVM 传输数据。
VO -A Value Object [1][2] represents itself 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.
VO -值对象 [1][2] 表示其自身是一组固定的数据,类似于 Java 枚举。值对象的标识基于它们的状态而不是它们的对象标识并且是不可变的。一个真实世界的例子是 Color.RED、Color.BLUE、SEX.FEMALE 等。
POJO vs JavaBeans
POJO 与 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.
[1] POJO 的 Java-Beanness 是它的私有属性都是通过符合 JavaBeans 约定的公共 getter 和 setter 访问的。例如
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.
[2] JavaBeans 必须实现 Serializable 并具有无参数构造函数,而在 POJO 中没有这些限制。
回答by Olcay Tarazan
Basically,
基本上,
DTO: "Data transfer objects " can travel between seperate layers in software architecture.
DTO:“数据传输对象”可以在软件架构中的不同层之间传输。
VO: "Value objects " hold a object such as Integer,Money etc.
VO:“值对象”持有一个对象,例如 Integer、Money 等。
POJO: Plain Old Java Object which is not a special object.
POJO:Plain Old Java Object,它不是一个特殊的对象。
Java Beans: requires a Java Class
to be serializable, have a no-arg
constructor and a getter and setter for each field
Java Beans:需要Java Class
可序列化,no-arg
每个字段都有一个构造函数和一个 getter 和 setter
回答by Suraj Kalokhe
POJO: It is a java file(class) which doesn't extend or implement any other java file(class).
POJO:它是一个 java 文件(类),它不扩展或实现任何其他 java 文件(类)。
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.
Bean:它是一个 java 文件(类),其中所有变量都是私有的,方法是公共的,并且使用适当的 getter 和 setter 来访问变量。
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).
普通类:它是一个 java 文件(类),它可能由公共/私有/默认/受保护变量组成,并且可能会或可能不会扩展或实现另一个 java 文件(类)。
回答by ASHISH KUMAR SINGH
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..
普通类-这意味着任何类定义在 Java 中都是正常的,这意味着您创建不同类型的方法属性等。
Bean -Bean 什么都不是,它只是使用此 bean 的特定类的对象,您可以像访问对象一样访问您的 java 类。.
and after that talk about last one POJO
然后说说最后一个 POJO
POJO- POJOis 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.
POJO- POJO是没有任何服务的类,它只有一个默认构造函数和私有属性,以及用于设置与 setter 和 getter 方法对应的值的那些属性。它是普通 Java 对象的简写形式。
回答by Atul Jain
- 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.
- 值对象:当需要根据对象的值来衡量对象的相等性时使用。
- 数据传输对象:一次将具有多个属性的数据从客户端跨层传递到服务器,以避免多次调用远程服务器。
- 简单的旧 Java 对象:它就像一个简单的类,它有属性,公共无参数构造函数。正如我们为 JPA 实体声明的那样。
difference-between-value-object-pattern-and-data-transfer-pattern