- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Pattern - Transfer Object Pattern
The Transfer Object pattern is used when we want to pass data with multiple attributes in one shot from cpent to server. Transfer object is also known as Value Object. Transfer Object is a simple POJO class having getter/setter methods and is seriapzable so that it can be transferred over the network. It does not have any behavior. Server Side business class normally fetches data from the database and fills the POJO and send it to the cpent or pass it by value. For cpent, transfer object is read-only. Cpent can create its own transfer object and pass it to server to update values in database in one shot. Following are the entities of this type of design pattern.
Business Object - Business Service fills the Transfer Object with data.
Transfer Object - Simple POJO having methods to set/get attributes only.
Cpent - Cpent either requests or sends the Transfer Object to Business Object.
Implementation
We are going to create a StudentBO as Business Object,Student as Transfer Object representing our entities.
TransferObjectPatternDemo, our demo class, is acting as a cpent here and will use StudentBO and Student to demonstrate Transfer Object Design Pattern.
Step 1
Create Transfer Object.
StudentVO.java
pubpc class StudentVO { private String name; private int rollNo; StudentVO(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } pubpc String getName() { return name; } pubpc void setName(String name) { this.name = name; } pubpc int getRollNo() { return rollNo; } pubpc void setRollNo(int rollNo) { this.rollNo = rollNo; } }
Step 2
Create Business Object.
StudentBO.java
import java.util.ArrayList; import java.util.List; pubpc class StudentBO { //pst is working as a database List<StudentVO> students; pubpc StudentBO(){ students = new ArrayList<StudentVO>(); StudentVO student1 = new StudentVO("Robert",0); StudentVO student2 = new StudentVO("John",1); students.add(student1); students.add(student2); } pubpc void deleteStudent(StudentVO student) { students.remove(student.getRollNo()); System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database"); } //retrive pst of students from the database pubpc List<StudentVO> getAllStudents() { return students; } pubpc StudentVO getStudent(int rollNo) { return students.get(rollNo); } pubpc 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 the StudentBO to demonstrate Transfer Object Design Pattern.
TransferObjectPatternDemo.java
pubpc class TransferObjectPatternDemo { pubpc 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); student.setName("Michael"); studentBusinessObject.updateStudent(student); //get the student student = studentBusinessObject.getStudent(0); System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); } }
Step 4
Verify the output.
Student: [RollNo : 0, Name : Robert ] Student: [RollNo : 1, Name : John ] Student: Roll No 0, updated in the database Student: [RollNo : 0, Name : Michael ]Advertisements