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

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

问题描述

看过一些类似的问题:

  • 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?

推荐答案

JavaBeans



JavaBean是遵循Sun定义的 JavaBeans约定的类。维基百科对 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:


JavaBeans是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类,对象类必须遵守有关方法命名,构造和行为的某些约定。这些约定使得可以使用可以使用,重用,替换和连接JavaBeans的工具。

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和其他方法(所谓的访问器方法和mutator方法)访问类属性,遵循标准命名惯例。这允许在框架内轻松自动检查和更新bean状态,其中许多包括用于各种类型属性的自定义编辑器。

  • 该类应该是可序列化的。这允许应用程序和框架以独立于VM和平台的方式可靠地保存,存储和恢复bean的状态。

因为这些需求主要表示为约定而不是实现接口,所以一些开发人员将JavaBeans视为遵循特定命名约定的普通旧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



普通旧Java对象或POJO是一个术语,最初用于指定一个简单的轻量级Java对象,而不是实现任何 javax。 ejb 接口,而不是重量级EJB 2.x(尤其是实体Bean,无状态会话Bean并不是那么糟糕的IMO)。今天,该术语用于任何没有额外内容的简单对象。同样,Wikipedia在定义 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
Object的首字母缩写。该名称用于强调
,有问题的对象是
普通Java对象,而不是特殊的
对象,特别是
Enterprise JavaBean(特别是在$ b之前) $ b EJB 3)。这个词由Martin
Fowler,Rebecca Parsons和Josh
MacKenzie于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:


我们想知道为什么人们如此反对在他们的
系统中使用常规对象并得出结论是
,因为简单的对象缺少一个花哨的
名称。所以我们给了他们一个,它是
很好地接受了。

这个术语延续了b $ b旧版术语的模式
不使用花哨的新功能,例如
电话中的
POTS(普通老式电话服务),以及用C ++ $ b $定义的PODS(普通旧数据
结构) b但在Perl中仅使用C语言功能和
POD(普通旧文档)。

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,它是
serializable,有一个无参数的
构造函数,并允许使用getter和setter
方法访问
属性。 Enterprise JavaBean不是
a单一类,而是整个组件
模型(同样,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提供框架中使用的
功能,以及
更多选择哪些区域为$实际上需要b $ b功能。
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

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:


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

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

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 Dirk Riehle



数据传输对象



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

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),以前称为值对象或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天全站免登陆