Design Pattern

Transfer Object Pattern – Core J2EE Patterns

It is one of the Java EE design patterns. We need Transfer Object when we need to pass the data across various attributes in a packet to the server. Value Object is another name for transfer object. The transfer object is just a class of POJO which has a method of the getter and setter. It is serializable which means we can transfer it through the network.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

Transfer Object Pattern

The transfer object does not acquire any behavior. The class known as server-side business is generally responsible for fetching data and filling the POJO then, sending it to the respective client or passing it the value. Client objects, the object is in read-only mode. The clients are capable for creating their own transfer objects. They can even pass object to servers in order to update the values in databases in one go.

UML Class Diagram of the Transfer Object

Let’s see the following class diagram of the transfer object.

It requires certain components to implement the transfer object design pattern. Each of these components has certain tasks to perform and certain problems to cater. These components include client, business object, and transfer object. The client object is responsible for representing the client of the enterprise bean.

  • The client can be the end user of the app.
  • The business object creates a transfer object and returns it to the respective client, when or if requested. The business object receives the data from the client in the form of a transfer object. It also uses this data in order to perform an update.
  • The transfer object is, in fact, a Javascript. The class known as Transfer object is able to provide a constructor which may accept all the needed attributes in order to create the transfer object. The constructor also accepts the entire entity bean attribute values that a Transfer object holds.

Sample Implementation of the Transfer Object

Let’s see the following sample implementation of the Transfer Object.

Step 1: Let’s create Transfer Object.

EmployeeVO.java

/**
 * 
 */
package com.doj.patterns.j2ee.transferobject;

/**
 * @author Dinesh.Rajput
 *
 */
public class EmployeeVO {
	
	private String name;
	private int empNo;
	
	public EmployeeVO(String name, int empNo) {
		super();
		this.name = name;
		this.empNo = empNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmpNo() {
		return empNo;
	}
	public void setEmpNo(int empNo) {
		this.empNo = empNo;
	}
	
}

Step 2: Let’s create Business Object.

EmployeeBO.java

/**
 * 
 */
package com.doj.patterns.j2ee.transferobject;

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

/**
 * @author Dinesh.Rajput
 *
 */
public class EmployeeBO {
	
	List employees;

	public EmployeeBO(){
		employees = new ArrayList<>();
		EmployeeVO employee1 = new EmployeeVO("Dinesh",0);
		EmployeeVO employee2 = new EmployeeVO("Arnav",1);
		employees.add(employee1);
		employees.add(employee2);		
	}
	public void deleteEmployee(EmployeeVO employee) {
		employees.remove(employee.getEmpNo());
		System.out.println("Employee: Roll No " + employee.getEmpNo() + ", deleted from database");
	}

	public List getAllEmployees() {
		return employees;	
	}

	public EmployeeVO getEmployee(int empNo) {
		return employees.get(empNo);
	}

	public void updateEmployee(EmployeeVO employee) {
		employees.get(employee.getEmpNo()).setName(employee.getName());
		System.out.println("Employee: Emp No " +employee.getEmpNo() +", updated in the database");
	}
}	

Step 3: Let’s create a demo class and use the EmployeeBO to demonstrate Transfer Object Design Pattern.

TransferObjectPatternDemo.java

/**
 * 
 */
package com.doj.patterns.j2ee.transferobject;

/**
 * @author Dinesh.Rajput
 *
 */
public class TransferObjectPatternDemo {
	
	public static void main(String[] args) {
		EmployeeBO EmployeeBusinessObject = new EmployeeBO();

		//print all Employees
		for (EmployeeVO employee : EmployeeBusinessObject.getAllEmployees()) {
			System.out.println("Employee: [Emp No : " + employee.getEmpNo() + ", Name : " + employee.getName() + " ]");
		}

		//update Employee
		EmployeeVO employee = EmployeeBusinessObject.getAllEmployees().get(0);
		employee.setName("Anamika");
		EmployeeBusinessObject.updateEmployee(employee);

		//get the Employee
		employee = EmployeeBusinessObject.getEmployee(0);
		System.out.println("Employee: [Emp No : " + employee.getEmpNo() + ", Name : " + employee.getName() + " ]");	
	}
}

Step 4: Let’s run this demo class and verify the output.

Employee: [Emp No : 0, Name : Dinesh ]
Employee: [Emp No : 1, Name : Arnav ]
Employee: Emp No 0, updated in the database
Employee: [Emp No : 0, Name : Anamika ]

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago