1. Overview
1.概述
In this tutorial, we’ll learn what Data Transfer Object (DTO), Value Object (VO), Plain Old Java Object (POJO), and JavaBeans are. We will look at the differences between them and understand which type to use and when.
在本教程中,我们将学习什么是数据传输对象(DTO)、价值对象(VO)、普通Java对象(POJO)和JavaBeans。我们将研究它们之间的区别,并了解何时使用哪种类型。
2. Plain Old Java Object
2.普通老式的Java对象
POJO, also known as Plain Old Java Object, is an ordinary Java object that does not have references to any particular framework. It’s a term used to refer to a simple, lightweight Java object.
POJO,也被称为Plain Old Java Object,是一种普通的Java对象,它没有对任何特定框架的引用。它是一个用来指代简单、轻量级Java对象的术语。
A POJO does not use any naming convention for properties and methods.
一个POJO不使用任何属性和方法的命名规则。
Let’s define a basic EmployeePOJO object that has three properties:
让我们定义一个基本的EmployeePOJO对象,它有三个属性。
public class EmployeePOJO {
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeePOJO(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String name() {
return this.firstName + " " + this.lastName;
}
public LocalDate getStart() {
return this.startDate;
}
}
As we can see, the above Java object defines the structure to represent an employee and does not depend on any framework.
我们可以看到,上述Java对象定义了代表雇员的结构,并且不依赖于任何框架。
3. JavaBeans
3.JavaBeans
3.1. What Is a JavaBean?
3.1.什么是JavaBean?
A JavaBean is mostly like a POJO, with some strict set of rules on how to implement it.
JavaBean大多像一个POJO,有一些关于如何实现它的严格规则。
The rules specify that it should be serializable, have a null constructor, and allow access to variables using methods that follow the getX() and setX() convention.
这些规则规定,它应该是可序列化的,有一个空的构造函数,并允许使用遵循getX()和setX()惯例的方法访问变量。
3.2. POJO as a JavaBean
3.2.作为JavaBean的POJO
Since a JavaBean is essentially a POJO, let’s convert EmployeePOJO to a JavaBean by implementing the necessary bean rules:
由于JavaBean本质上是一个POJO,让我们通过实现必要的Bean规则将EmployeePOJO转换为一个JavaBean。
public class EmployeeBean implements Serializable {
private static final long serialVersionUID = -3760445487636086034L;
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeeBean() {
}
public EmployeeBean(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// additional getters and setters
}
Here, in order to convert the POJO into a JavaBean, we’ve implemented the Serializable interface, marked properties as private, and used getter/setter methods to access the properties.
在这里,为了将POJO转换为JavaBean,我们实现了Serializable接口,将属性标记为private,并使用getter/setter方法来访问这些属性。
4. DTO
4. DTO
4.1. The DTO Pattern
4.1.DTO模式
A DTO, also referred to as Data Transfer Object, encapsulates values to carry data between processes or networks.
DTO也被称为数据传输对象,它封装了用于在进程或网络之间传输数据的值。
This helps in reducing the number of methods called. By including multiple parameters or values in a single call, we reduce the network overhead in remote operations.
这有助于减少调用方法的数量。通过在一次调用中包括多个参数或值,我们减少了远程操作的网络开销。
One more advantage of this pattern is the encapsulation of the serialization’s logic. It lets the program store and transfer data in a specific format.
这种模式的另一个优点是对序列化的逻辑进行了封装。它让程序以特定的格式存储和传输数据。
A DTO does not have any explicit behavior. It basically helps in making the code loosely coupled by decoupling the domain models from the presentation layer.
DTO没有任何明确的行为。它基本上有助于通过将领域模型与表现层解耦来实现代码的松散耦合。
4.2. How to Use DTO?
4.2.如何使用DTO?
DTOs have flat structures without any business logic. They use the same format as that of POJOs. A DTO only contains storage, accessors, and methods related to serializing or parsing.
DTOs有扁平结构,没有任何业务逻辑。它们使用与POJOs相同的格式。一个DTO只包含存储、访问器和与序列化或解析有关的方法。
DTOs basically map to a domain model and thus send data to a method or a server.
DTOs基本上映射到一个领域模型,从而将数据发送到一个方法或一个服务器。
Let’s create the EmployeeDTO that groups all the necessary details to create an employee. We’ll send this data to a server in a single request that optimizes the interactions with the API:
让我们创建EmployeeDTO,将所有必要的细节分组以创建一个雇员。我们将在一个单一的请求中把这些数据发送到服务器,以优化与API的交互。
public class EmployeeDTO {
private String firstName;
private String lastName;
private LocalDate startDate;
// standard getters and setters
}
The above DTO interacts with different services and handles the flow of data. This DTO pattern can be used in any service without any framework limitations.
上述DTO与不同的服务进行交互并处理数据流。这种DTO模式可以在任何服务中使用,没有任何框架限制。
5. VO
5. VO
VO, also known as the Value Object, is a special type of object that can hold values such as java.lang.Integer and java.lang.Long.
VO,又称价值对象,是一种特殊类型的对象,可以容纳诸如java.lang.Integer和java.lang.Long的值。。
A VO should always override the equals() and hashCode() methods. VOs generally encapsulate small objects such as numbers, dates, strings, and more. They follow the value semantics, i.e., they directly change the object’s value and pass copies around instead of references.
VO应该总是覆盖equals()和hashCode()方法。VO通常封装小型对象,如数字、日期、字符串等。它们遵循价值语义,也就是说,它们直接改变对象的值,并传递副本而不是引用。
It’s a good practice to make Value Objects immutable. The change in values occurs only by creating a new object and not by updating values in the old object itself. This helps in understanding the implicit contract that two Value Objects created equal should remain equal.
使价值对象不可变是一个很好的做法。值的变化只发生在创建一个新的对象上,而不是通过更新旧对象本身的值。这有助于理解隐含的契约,即两个平等创建的Value Objects应该保持平等。
Let’s define EmployeeVO and override the equals() and hashCode() methods:
让我们定义EmployeeVO并重写equals()和hashCode()方法。
public class EmployeeVO {
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeeVO(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
// Getters
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
EmployeeVO emp = (EmployeeVO) obj;
return Objects.equals(firstName, emp.firstName)
&& Objects.equals(lastName, emp.lastName)
&& Objects.equals(startDate, emp.startDate);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, startDate);
}
}
6. Conclusion
6.结语
In this article, we saw the definitions of POJO, JavaBeans, DTO, and Value Objects. We also saw how some frameworks and libraries harness the JavaBean naming conventions and how to convert a POJO to a JavaBean. We also had a look at the DTO pattern and Value Objects along with their usage in different scenarios.
在这篇文章中,我们看到了POJO、JavaBeans、DTO和Value Objects的定义。我们还看到了一些框架和库如何利用JavaBean的命名规则,以及如何将POJO转换为JavaBean。我们还看到了DTO模式和Value Objects,以及它们在不同情况下的用法。
As always, the code for these examples is available over on GitHub.
像往常一样,这些例子的代码可以在GitHub上找到over。