<p>It is one of the <strong><a href="https://dineshonjava.com/core-j2ee-patterns-best-design-practices/">Java EE design patterns</a></strong>. We need <strong>Transfer Object</strong> when we need to pass the data across various attributes in a packet to the server. <strong>Value Object</strong> is another name for transfer object. The <strong>transfer object</strong> 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.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput"><br />
<img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://i0.wp.com/dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern.jpg?resize=243%2C300&;ssl=1" alt="Spring-5-Design-Pattern" width="243" height="300" /></a></div>
</div>
<h2>Transfer Object Pattern</h2>
<p>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.</p>
<h2>UML Class Diagram of the Transfer Object</h2>
<p>Let&#8217;s see the following class diagram of the transfer object.</p>
<p><img class="aligncenter size-full wp-image-4066" src="https://dineshonjava.com/wp-content/uploads/2018/01/Transfer-Object.gif" alt="Transfer Object" width="491" height="245" /></p>
<div style="text-align: left;">
<blockquote>
<p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/business-delegate/" href="https://dineshonjava.com/business-delegate/">Business Delegate Pattern</a></li>
<li><a title="https://dineshonjava.com/service-locator/" href="https://dineshonjava.com/service-locator/">Service Locator Pattern</a></li>
<li><a title="https://dineshonjava.com/session-facade/" href="https://dineshonjava.com/session-facade/">Session Facade Pattern</a></li>
<li><a title="https://dineshonjava.com/business-object/" href="https://dineshonjava.com/business-object/">Business Object Pattern</a></li>
<li><a title="https://dineshonjava.com/composite-entity-pattern/" href="https://dineshonjava.com/composite-entity-pattern/">Composite Entity Pattern</a></li>
</ul>
</blockquote>
</div>
<p>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.</p>
<ul>
<li><strong>The client</strong> can be the end user of the app.</li>
<li><strong>The business object</strong> 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.</li>
<li><strong>The transfer object</strong> 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.</li>
</ul>
<h2>Sample Implementation of the Transfer Object</h2>
<p>Let&#8217;s see the following sample implementation of the Transfer Object.</p>
<h3>Step 1: Let&#8217;s create Transfer Object.</h3>
<p><strong>EmployeeVO.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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; 
	} 
	 
} 
 
</pre>
<h3>Step 2: Let&#8217;s create Business Object.</h3>
<p><strong>EmployeeBO.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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"); 
	} 
}	 
 
</pre>
<h3>Step 3: Let&#8217;s create a demo class and use the EmployeeBO to demonstrate Transfer Object Design Pattern.</h3>
<p><strong>TransferObjectPatternDemo.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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() + " ]");	 
	} 
} 
 
</pre>
<h3>Step 4: Let&#8217;s run this demo class and verify the output.</h3>
<pre class="highlight">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 ] 
 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/composite-entity-pattern/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/data-access-object/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4065'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…