Data transfer object(DTO) Design Pattern in java examples


This post covers an 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 also called as Transfer Object or Value Object

create Connection Pool in java with examples

Why Data transfer Objects pattern required?

We must retrieve data from the database in the majority of java projects from Database. In a method of a java class, you query the databases from your application (for example, select employee id, employee name from employee). These code may be a web application or a standalone application in this case (applet or Eclipse RCP client). Assume that the programme 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, Solution is, 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 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 plin java class with 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 java object is transferred between different JVM’s or machines, The object must be serializable.

for any java object to make serializable, we have to implement java.io.Serializable interface, which has no methods to implement.These also called as 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 and Setter and getters

  • Class names are nouns or noun phases

  • Suffix “DTO” or “TO” 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 dto folder, For example, complete package name is org.company.security.dto

Conclusion

TO sumup, DTO or VO or TO design pattern are used

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.