Data transfer object(DTO) Design Pattern in java examples

This post covers a Data transfer object(DTO) covers an example and naming conventions. The Data Transfer Object (DTO) design pattern is one of the design patterns used to transfer data from one system to another.

These are also called Transfer Object or Value Object

Why is the Data Transfer Objects pattern required?

We must retrieve data from the database in the majority of java projects Database. In a method of a java class, you query the databases from your application (for example, select employee id, employee name from employee). This code may be a web application or a standalone application in this case (applet or Eclipse RCP client). Assume that the program is run on Host-1 and that the MySQL database is stored on Host-2.

When you get employee details from the application, the application queries the employee id for the first time, then the employee name for the second time, and so on for all fields.

Instead, the Solution is, that we can build a java POJO class that stores all of the fields, reducing network latency and increasing efficiency.

Advantages of DTO or VO classes:

  • To reduce the network traffic, we will create a java object which has member variables holding each row of data and returns each object or list of objects.
  • Data transfer is made easy and network traffic is reduced.
  • Reduce code duplication with this approach

Data Transfer Object design pattern Example

This pattern is a plain java class with a setter and getter.

  • declare private member variables
  • Provide setter and getter for member variables
public class Employee {

 private long mEmployeeId;
 private String mEmployeeName;

 public void setEmployeeId(long pEmployeeId) {
  this.mEmployeeId = pEmployeeId;
 }

 public long getEmployeeId() {
  return this.mEmployeeId;
 }

 public void setEmployeeName(String pEmployeeName) {
  this.mEmployeeName = pEmployeeName;
 }

 public String getEmployeeName() {
  return this.mEmployeeName;

 }
}

How to make the java object serializable In java, whenever a java object is transferred between different JVMs or machines, The object must be serializable.

for any java object to make serializable, we have to implement the java.io.Serializable interface, which has no methods to implement. These are also called marker interfaces.

So, how do we achieve serializable without implementing methods?

JVM assumes that whenever objects are implemented with Serializable interfaces. it is eligible for the serialization process. JVM checks each object for using the method (object instanceOf Serializable) for the process.

Here is an example

public class Employee implements Serializable{
     private long mEmployeeId;
 private String mEmployeeName;

 public void setEmployeeId(long pEmployeeId) {
  this.mEmployeeId = pEmployeeId;
 }

 public long getEmployeeId() {
  return this.mEmployeeId;
 }

 public void setEmployeeName(String pEmployeeName) {
  this.mEmployeeName = pEmployeeName;
 }

 public String getEmployeeName() {
  return this.mEmployeeName;
 }
}

Data transfer Object naming convention

  • DTO contains POJO with Setter and getters
  • Class names are nouns or noun phrases
  • Suffix “DTO” or “Too” or “VO” to the class names but these are not compulsory For example, if the object is a User, the class name should be UserDTO.
  • all the DTO classes are placed in the dto folder, For example, the complete package name is org.company.security.dto

Conclusion

To sum up, DTO or VO, or TO design patterns are used.