Java DTO Pattern Design Example

DTO or Data Transfer Object also known as Value Object(VO) is a simple Java object or POJO(Plain Old Java Object) used during transferring data between different layers/tiers in the application. It does not have any behaviour of its own, except for storage and retrieval. In multi-tiered application the calls between different layers are usually expensive, DTO helps in reducing the number of method calls made. For e.g. if we want to insert several related columns into database tables, instead of making several calls we can pass/transfer on the object consisting of all data and accomplish with a single call.
A better definition you can read from here.
DTO is designed with all appropriate attributes and getter, setter methods. It is often Serializable, so that it could be transferred over the network.
Sample DTO class design:

import java.io.Serializable;
import java.util.Date;

public class EmployeeDTO implements Serializable {

 private int id;
 private String name;
 private Date birthDate;

 public EmployeeDTO() {
 }

 public EmployeeDTO(int id, String name, Date birthDate) {
  this.id = id;
  this.name = name;
  this.birthDate = birthDate;
 }

 public void setId(int id) {
  this.id = id;
 }

 public void setName(String name) {
  this.name = name;
 }

 public void setBirthDate(Date birthDate) {
  this.birthDate = birthDate;
 }

 public int getId() {
  return id;
 }

 public String getName() {
  return name;
 }

 public Date getBirthDate() {
  return birthDate;
 }

}

No comments:

Post a Comment