Transfer Object Pattern
The Transfer Object Pattern is used to pass data with multiple attributes from the client to the server all at once. A transfer object is also called a value object. A transfer object is a simple POJO class with getter/setter methods; it is serializable, so it can be transmitted over the network. It does not have any behavior. The business class on the server side usually reads data from the database, then fills the POJO and sends it to the client or passes it by value. To the client, the transfer object is read-only. The client can create its own transfer object and pass it to the server to update values in the database all at once. The following are the entities of this design pattern.
- Business Object - The business service that fills data into the transfer object.
- Transfer Object - A simple POJO with only setter/getter methods for attributes.
- Client - The client can send requests or send a transfer object to the business object.
Summary
Intent
Used to simplify data transmission between network or application layers. It encapsulates data by creating a class with multiple attributes that represent the data to be transferred.
Main problem solved
- Solves the efficiency and encapsulation problems when data is transferred between layers in distributed systems, especially in multi-tier applications.
Use Cases
- When large amounts of data need to be transferred between the client and the server, especially in Web services or remote procedure calls (RPC).
Implementation
- Transfer Object class: Defines a class with multiple attributes that represent the data to be transferred.
- Data access layer: Responsible for extracting data from a database or other data source and filling it into the transfer object.
- Business logic layer: Optional; handles business logic and may need to use transfer objects.
- Presentation layer or client: Uses transfer objects to display data or as a data carrier for user input.
Key code
- Transfer Object class: Contains attributes and possibly business logic for data encapsulation and transmission.
Application Example
- Web application: The presentation layer obtains or sends data from/to the server through transfer objects.
Advantages
- Reduce network communication overhead: Reduces the number of network requests by transmitting multiple pieces of data at once.
- Encapsulation: The transfer object acts as an encapsulated carrier of data, hiding the internal data structure.
- Easy to Maintain: Centrally manages transferred data for easy modification and extension.
Disadvantages
- Overuse: May increase system complexity due to creating too many transfer objects.
Usage recommendation
- Consider using the Transfer Object Pattern when large amounts of data need to be transferred between different layers of a multi-tier application.
Notes
- Transfer objects should remain lightweight and avoid containing too much business logic.
Main roles included
-
Transfer Object Class:
-
Data Access Layer:
-
Business Logic Layer (optional):
-
Presentation Layer or Client:
The Transfer Object Pattern provides an effective way to reduce the number of network communications and simplify the data transfer process by encapsulating transferred data into an object.
Create a class that implements
We will create a business object StudentBO and a transfer object StudentVOboth of which represent our entities.
TransferObjectPatternDemo The class here acts as a client and will use StudentBO and Student to demonstrate the Transfer Object design pattern.
Step 1
Create the 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 the business object.
StudentBO.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
StudentBO
{
//
The 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 the list of students from the 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.
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 a student
StudentVO
student
=
studentBusinessObject
.
getAllStudents
(
)
.
get
(
0
)
;
student
.
setName
(
"
Michael
"
)
;
studentBusinessObject
.
updateStudent
(
student
)
;
//
Get a student
studentBusinessObject
.
getStudent
(
0
)
;
System
.
out
.
println
(
"
Student: [RollNo :
"
student
.
getRollNo
(
)
"
, Name :
"
student
.
getName
(
)
"
]
"
)
;
}
}
Step 4
Run the program, output result:
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]