Latest web development tutorials

Transfer Object Model

Transfer Object Model (Transfer Object Pattern) for a one-time transfer of data with multiple attributes from the client to the server. Transport object is also known as value objects. Transfer the object is a simple POJO class getter / setter methods, it is serializable, so it can be transmitted through the network. It has no behavior. Business class server typically read data from the database, and then fill POJO, and sends it to the client or pass it by value. For clients, the transfer object is read-only. Clients can create their own transport object, and pass it to the server in order to update the database in one-time value. The following is an entity of this design pattern.

  • Business Objects (Business Object) - transport service for the business object fill data.
  • Transfer Object (Transfer Object) - a simple POJO, only set / get method attribute.
  • Client (Client) - The client can send a request or send a transmission object to the business object.

achieve

We will create a business object asStudentBOand as an object of transmissionStudentVO,they represent our entity.

TransferObjectPatternDemo,our demo class here as a client will be used to demonstrateStudentBOandStudentTransfer Object design pattern.

Transfer Object pattern UML diagram

step 1

Create a transfer object.

StudentVO.java

public class StudentVO {
   private String name;
   private int rollNo;

   StudentVO (String name, int rollNo) {
      this.name = name;
      this.rollNo = rollNo;
   }

   public String getName () {
      return name;
   }

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

   public int getRollNo () {
      return rollNo;
   }

   public void setRollNo (int rollNo) {
      this.rollNo = rollNo;
   }
}

Step 2

Create business objects.

StudentBO.java

import java.util.ArrayList;
import java.util.List;

public class StudentBO {
	
   // List is a database as a List <StudentVO> students;

   public StudentBO () {
      students = new ArrayList <StudentVO> ();
      StudentVO student1 = new StudentVO ( "Robert", 0);
      StudentVO student2 = new StudentVO ( "John", 1);
      students.add (student1);
      students.add (student2);		
   }
   public void deleteStudent (StudentVO student) {
      students.remove (student.getRollNo ());
      System.out.println ( "Student: Roll No" 
      + Student.getRollNo () + ", deleted from database");
   }

   // Retrieved from the database list of students public List <StudentVO> getAllStudents () {
      return students;
   }

   public StudentVO getStudent (int rollNo) {
      return students.get (rollNo);
   }

   public void updateStudent (StudentVO student) {
      students.get (student.getRollNo ()) setName (student.getName ()).;
      System.out.println ( "Student: Roll No" 
      + Student.getRollNo () + ", updated in the database");
   }
}

Step 3

UseStudentBOto demonstrate the transfer object design patterns.

TransferObjectPatternDemo.java

public class TransferObjectPatternDemo {
   public static void main (String [] args) {
      StudentBO studentBusinessObject = new StudentBO ();

      // Output 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).;
      student.setName ( "Michael");
      studentBusinessObject.updateStudent (student);

      // Get students studentBusinessObject.getStudent (0);
      System.out.println ( "Student: [RollNo:"
      + Student.getRollNo () + ", Name:" + student.getName () + "]");
   }
}

Step 4

Verify output.

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