关于java:DTO,VO,POJO,JavaBeans之间的区别?

Difference between DTO, VO, POJO, JavaBeans?

看到了一些类似的问题:

  • JavaBean和Pojo有什么区别?
  • 波霍(普通Java对象)和DTO(数据传输对象)有什么区别?

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


爪哇豆

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.

波乔

一个简单的旧Java对象或POJO是一个术语,最初被用来指定一个简单的轻量级Java对象,而不是实现任何EDCOX1×0接口,而不是重量级EJB 2。(尤其是实体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."

这个术语延续了旧的技术术语不使用花哨的新功能,例如锅(普通的旧电话服务)电话和pods(普通老数据在C++中定义的结构但只使用C语言功能,以及Perl中的pod(普通老文档)。

这个词很有可能是因为需要一个共同的和容易的与之形成对比的可理解术语复杂的对象框架。一JavaBean是一个pojo可序列化,没有参数并允许访问使用getter和setter的属性方法。企业JavaBean不是单个类,但整个组件模型(同样,EJB3减少了企业JavaBeans的复杂性)。

当使用pojos的设计变得更常用的是,系统给波霍斯一些框架和更多选择哪些领域实际上需要功能。冬眠和春天就是例子。

< /块引用>价值客体

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

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.

数据传输对象

数据传输对象(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和VOS是一样的(但是福勒用VOS来表示我们看到的其他东西)。大多数时候,他们遵循JavaBeans惯例,因此也是JavaBeans。所有人都是波乔斯。


DTO VO VO

数据传输对象只是用于在层和层之间传输数据的数据容器。

  • 它主要包含属性。甚至可以在没有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.

DTO主要用于高效地通过网络传输数据,甚至可以从JVM传输到另一个JVM。

DTO通常是java.io.Serializable,以便在JVM之间传输数据。

VO-A值对象〔1〕〔2〕本身代表一组固定的数据,类似于Java枚举。值对象的标识是基于其状态而不是基于其对象标识的,并且是不可变的。一个现实世界的例子是color.red、color.blue、sex.female等。

波霍vs爪哇人

〔1〕POJO的Java BoeNess是其私有属性都是通过符合JavaBeans约定的公共吸气剂和设置器访问的。例如

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

〔2〕JavaBeans必须实现可序列化并具有无参数构造函数,而在POJO中没有这些限制。


基本上,

DTO:"数据传输对象"可以在软件体系结构中的不同层之间传输。

vo:"值对象"包含整数、货币等对象。

波霍:普通的Java对象,它不是一个特殊的对象。

JavaBeans:需要一个EDCOX1×2的可序列化,有一个EDOCX1×3的构造函数和每个字段的一个吸收器和设置器。


Java bean与EJB不一样。

Java 1中的JavaBeans规范是Sun试图允许Java对象在一个看起来像VB的IDE中操作的尝试。对于符合"Java bean"的对象,制定了规则:

  • 默认构造函数
  • 遵循正确命名约定的私有数据成员的getter和setter
  • 可串行化的
  • 也许是我忘记的其他人。
  • EJB随后出现。它们结合了分布式组件和事务模型,在一个容器中运行,该容器管理线程、池、生命周期并提供服务。它们与爪哇豆有很大的差距。

    DTOs是在Java上下文中出现的,因为人们发现EJB 1规范对数据库来说太"唠叨"了。人们不必对每个数据元素进行往返,而是将它们打包成Java bean,并将它们传送到周围。

    Pojos是对EJB的反应。


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

    Bean:它是一个Java文件(类),其中所有变量都是私有的,方法是公共的,适当的吸收器和设置器用于访问变量。

    正常班级:它是一个Java文件(类),它可以由公共/私有/默认/受保护的变量组成,它可以或不可以扩展或实现另一个Java文件(类)。


    第一次谈论

    正常类——这意味着任何类都定义了Java中的正常类型,它意味着创建不同类型的方法属性等。bean bean不是什么,它只是这个bean的特定类的一个对象,你可以访问与对象相同的Java类。

    然后再谈最后一个pojo

    pojo-pojo是一个没有任何服务的类,它只有一个默认的构造函数和私有属性,以及那些用于设置对应于setter和getter方法的值的属性。它是普通Java对象的短形式。


    • 值对象:当需要根据对象的值测量对象的相等性时使用。
    • 数据传输对象:一次将具有多个属性的数据跨层从客户机传递到服务器,以避免对远程服务器进行多个调用。
    • 普通的Java对象:它就像简单的类属性,没有公共的ARG构造函数。正如我们为JPA实体声明的那样。

    价值对象模式和数据传输模式的区别