Transfer object mode

Transfer Object Pattern is used to transfer data with multiple attributes from the client to the server at one time. Transfer objects are also called numeric objects. The transport object is a simple POJO class with a getter/setter method that is serializable so it can be transmitted over the network. It does not have any behavior. The server-side business class typically reads data from the database, populates the POJO, and sends it to the client or passes it by value. For the client, the transport object is read-only. The client can create its own transport object and pass it to the server to update the values in the database at once. The following are the entities of this design pattern.

  • Business Object - A business service that populates data for transport objects.
  • Transfer Object - Simple POJO, only the method of setting/getting properties.
  • Client - The client can send a request or send a transport object to a business object.
  • Step 1

    Create a transfer object.

    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; } }

    Step 2

    Create a business object.

    import java.util.ArrayList; import java.util.List; public class StudentBO { //List is treated as a database 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"); } //Retrieve student list from database 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

    Use StudentBO to demonstrate the transfer object design pattern.

    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

    Executing the program, outputting results:

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

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition