Difference between DTO, VO, POJO, 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 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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: DTO (Data Transfer Object) 是一种软件设计模式,用于在多个系统之间传递数据。它是一个简单的Java对象,用于封装数据,可以将数据从一个系统传递到另一个系统。 Entity是一个业务对象,表示数据库中的一张表,是数据库持久化对象,也是一种软件设计模式,用于表示业务对象。 POJO (Plain Old Java Object) 是指普通的Java对象,没有继承任何特殊类或实现任何特殊接口。它可能是一个Entity或DTO。 总的来说,DTO是用于在系统之间传递数据的对象,Entity是持久化对象,POJO是普通的Java对象。 ### 回答2: DTOVO、Entity和POJO(Plain Old Java Object)是开发中常见的四种对象,它们各有不同的作用和用途。 1. DTO(Data Transfer Object)数据传输对象 DTO是用于不同层之间数据传输的一种对象。通常在业务逻辑层和表示层之间传输,作为中转站,可以减少对数据库重复操作和网络传输次数,提高效率。 2. VO(Value Object)值对象 VO是一个类,它封装了某个方法调用的返回值或者给用户端展示的属性列表。它使用在业务逻辑层和表现层之间传递简单数据类型。VO属于供客户端使用,是用来展示数据的一种业务对象。 3. Entity Entity是实体类,它是用来与数据库相映射的一种对象。实体类一般与数据表的结构对应,每个实体都是一个持久化对象,实体中包含了要操作的数据和对数据的操作。它用于ORM(Object-Relational Mapping)关系映射,ORM技术将数据库表结构映射为面向对象的实体,对数据操作进行简化。 4. POJO(Plain Old Java Object)普通Java对象 POJO是一种普通的Java对象,它没有实现任何框架或者接口,不依赖任何第三方库,通用性比较强。POJO对象只有属性的get和set方法,用来简单地封装数据。它是用来承载业务数据或数据VO对象的一种对象。 总结: 在实际开发中,我们可能会用到DTOVO、Entity和POJO这几种对象。DTO用于不同层之间数据传输,VO用于业务层和表现层之间传输,Entity用于与数据库相映射,POJO用于承载业务数据或数据VO对象。我们需要根据实际开发需要,针对每种类型的对象,进行合理使用。 ### 回答3: DTOVO、Entity、POJO是Java编程中常见的概念,它们分别代表不同的数据模型。 DTO(Data Transfer Object)是一种数据传输对象,它主要用于不同层之间数据的传输,可以理解为数据的载体或者数据的容器。DTO通常包含多个属性,但不包含任何业务逻辑,DTO中的属性通常可以是各种类型的JavaBean、字符串或者Java基本数据类型。 VO(Value Object)是一种值对象,通常将数据从一个或多个Java Bean中提取出来,并将这些值组合成一个全新的对象。与DTO不同的是,VO在传输过程中不仅带有数据,还包含了一些描述这些数据的特征和属性的信息,通常是直接在Java Bean中提取出来的属性和方法。 Entity是一种实体对象,通常代表数据库中的一张表或者多张表的关联信息。Entity包含了所有的业务逻辑、数据库交互和持久化逻辑,并且通过ORM框架实现数据的 CRUD 操作。在项目中,Entity通常是Java对象和数据库表之间的桥梁。 POJO(Plain Old Java Object)是普通的Java对象,它是一种简单的Java类,没有任何限制,不继承或实现任何Java类或接口,也不需要遵循任何可重载构造函数的规则。POJO可以用来表示Java中的任何一种数据类型,通常不包含业务逻辑或任何框架相关的代码。 总的来说,DTOVO是用于数据传输的,而Entity和POJO则是用于数据持久化和业务逻辑的。DTOVO在传输过程中的区别比较难理解,需要在实际项目中进行实践和对比,而Entity和POJO是用来处理数据库操作的,需要符合设计模式和项目的实际需要。因此,在实际开发中,需要结合项目的实际情况选择使用不同的数据模型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值