DTO、VO、POJO、JavaBeans 的区别? [英] Difference between DTO, VO, POJO, JavaBeans?

查看:23
本文介绍了DTO、VO、POJO、JavaBeans 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看过一些类似的问题:

你能告诉我它们的使用环境吗?还是他们的目的?

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

推荐答案

JavaBeans

JavaBean 是遵循 JavaBeans 约定的类 由 Sun 定义.维基百科对 JavaBeans 是什么有一个很好的总结:

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:

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

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.

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

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.

所需的约定是:

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

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

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 或 POJO 是最初引入的一个术语,用于指定一个简单的轻量级 Java 对象,不实现任何 javax.ejb 接口,与重量级 EJB 2.x(尤其是实体 Beans,无状态会话 Bean 并不是那么糟糕的 IMO).今天,该术语用于任何没有额外内容的简单对象.同样,维基百科在定义 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:

POJO 是Plain Old Java 的首字母缩写词目的.名字是用来强调的所讨论的对象是一个普通的Java Object,不是特别的对象,特别是不是Enterprise JavaBean(尤其是在EJB 3).这个词是由马丁创造的福勒、丽贝卡·帕森斯和乔什2000 年 9 月的麦肯齐:

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:

我们想知道为什么人们如此反对在他们的系统并得出结论,这是因为简单的物体缺乏花哨姓名.所以我们给了他们一个很受欢迎."

该术语延续了技术的旧术语不要使用花哨的新功能,例如POTS(Plain Old Telephone Service)在电话和 PODS(Plain Old Data结构)在 C++ 中定义但仅使用 C 语言功能,并且Perl 中的 POD(Plain Old Documentation).

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.

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

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).

随着使用 POJO 的设计变得更常用的系统有出现给 POJO 一些框架中使用的功能和更多关于哪些领域的选择实际需要的功能.Hibernate 和 Spring 就是例子.

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.

值对象

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

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.

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

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.

您可以在 wiki德克·里尔.

数据传输对象

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

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

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.

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

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).

在传统的 EJB 架构中,DTO 有双重目的:首先,它们解决了实体 bean 不可序列化的问题;其次,它们隐含地定义了一个组装阶段,在这个阶段,视图将使用的所有数据在将控制权返回给表示层之前被提取并编组到 DTO 中.

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 是一回事(但正如我们所见,Fowler 使用 VO 来表示其他含义).大多数情况下,它们遵循 JavaBeans 约定,因此也是 JavaBeans.而且都是 POJO.


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、POJO、JavaBeans 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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