Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Transfer Object Pattern

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.


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

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Reduce network communication overhead: Reduces the number of network requests by transmitting multiple pieces of data at once.
  2. Encapsulation: The transfer object acts as an encapsulated carrier of data, hiding the internal data structure.
  3. Easy to Maintain: Centrally manages transferred data for easy modification and extension.

Disadvantages

Usage recommendation

Notes

Main roles included

  1. Transfer Object Class:

  2. Encapsulates the data to be transferred.
  3. - Encapsulates the data to be transferred.
    1. Data Access Layer:

    2. Provides data and may be responsible for filling data into transfer objects.
    3. - Provides data and may be responsible for filling data into transfer objects.
      1. Business Logic Layer (optional):

      2. Optional; handles business logic and may use transfer objects.
      3. - Optional; handles business logic and may use transfer objects.
        1. Presentation Layer or Client:

        2. Uses transfer objects to display data or send requests.
        3. - Uses transfer objects to display data or send requests.

          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 ]