Design Patterns - SPLessons

Transfer Object Pattern

Chapter 35

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Transfer Object Pattern

Transfer Object Pattern

shape Description

Transfer Object Pattern – Utilize a Transfer Object to typify the business information. A solitary technique call is utilized to send and recover the Transfer Object. At the point when the customer asks for the endeavor bean for the business information, the undertaking bean can develop the Transfer Object, populate it with its trait values, and pass it by quality to the customer.

Clients require more than one quality from a venture bean. To lessen the quantity of remote calls and to maintain a strategic distance from the related overhead, it is best to utilize Transfer Objects to transport the information from the venture bean to its customer.

At the point when an undertaking bean utilizes a Transfer Object, the customer makes a solitary remote strategy summon to the endeavor bean to ask for the Transfer Object rather than various remote technique calls to get singular trait values. The venture bean then builds another Transfer Object occurrence, duplicates values into the article and returns it to the customer. The customer gets the Transfer Object and can then conjure getter techniques on the Transfer Object to get the individual quality qualities from the Transfer Object. Then again, the execution of the Transfer Object might be with the end goal that it makes all properties open. Since the Transfer Object is passed by quality to the customer, all calls to the Transfer Object example are nearby calls rather than remote strategy summons.

shape Example

Following is an example for the Transfer Object Pattern.

public class StudentVO {
   private String name;
   private int rollNo;
   StudentVO(String name, int rollNo){ = name;
      this.rollNo = rollNo;
   public String getName() {
      return name;
   public void setName(String name) { = name;
   public int getRollNo() {
      return rollNo;
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;

Transfer Object Pattern – Here created the transfer object, The TransferObject is a discretionary serializable Java object alluded to as a Transfer Object. A Transfer Object class may give a constructor that acknowledges all the obliged ascribes to make the Transfer Object. The constructor may acknowledge all element bean trait values that the Transfer Object is intended to hold. Ordinarily, the individuals in the Transfer Object are characterized as open, along these lines wiping out the requirement for get and set strategies. On the off chance that some assurance is essential, then the individuals could be characterized as ensured or private, and strategies are given to get the qualities. By offering no strategies to set the qualities, a Transfer Object is shielded from adjustment after its creation. On the off chance that lone a couple of individuals are permitted to be adjusted to encourage redesigns, then strategies to set the qualities can be given. In this way, the Transfer Object creation differs relying upon an application’s prerequisites. It is a configuration decision with respect to whether the Transfer Object’s characteristics are private and got to by means of getters and setters, or every one of the traits are made open.

import java.util.ArrayList;
import java.util.List;
public class StudentBO {
   //list is working as a database
   List<StudentVO> students;
   public StudentBO(){
      students = new ArrayList<StudentVO>();
      StudentVO student1 = new StudentVO("SPLESSON",0);
      StudentVO student2 = new StudentVO("John",1);
   public void deleteStudent(StudentVO student) {
      System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database");
   //retrive list of students from the database
   public List<StudentVO> getAllStudents() {
      return students;
   public StudentVO getStudent(int rollNo) {
      return students.get(rollNo);
   public void updateStudent(StudentVO student) {
      System.out.println("Student: Roll No " + student.getRollNo() +", updated in the database");

Transfer Object Pattern – Here created the business object, The BusinessObject speaks to a part in this pattern that can be satisfied by a session bean, an element bean, or a Data Access Object (DAO). The BusinessObject is in charge of making the Transfer Object and returning it to the customer upon solicitation. The BusinessObject may likewise get information from the customer as a Transfer Object and utilize that information to play out an overhaul.

public class TransferObjectPatternDemo {
   public static void main(String[] args) {
      StudentBO studentBusinessObject = new StudentBO();
      //print all students
      for (StudentVO student : studentBusinessObject.getAllStudents()) {
         System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
      //update student
      StudentVO student = studentBusinessObject.getAllStudents().get(0);
      //get the student
      student = studentBusinessObject.getStudent(0);
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");

Here main has been written, Output will be as follows.

Student: [RollNo : 0, Name : SPLESSON ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Sachin ]


shape Key Points

  • Transfer Object Pattern Reduces Code Duplication.
  • Transfer Object is a simple POJO class having getter/setter methods.