Browser docs

Transaction Script

Intent

Transaction Script organizes business logic by procedures where each procedure handles a single request from the presentation.

Explanation

Real world example

You need to create a hotel room booking system. Since the requirements are quite simple we intend to use the Transaction Script pattern here.

In plain words

Transaction Script organizes business logic into transactions that the system needs to carry out.

Programmatic example

The Hotel class takes care of booking and cancelling room reservations.

 1@Slf4j
 2public class Hotel {
 3
 4  private final HotelDaoImpl hotelDao;
 5
 6  public Hotel(HotelDaoImpl hotelDao) {
 7    this.hotelDao = hotelDao;
 8  }
 9
10  public void bookRoom(int roomNumber) throws Exception {
11
12    Optional<Room> room = hotelDao.getById(roomNumber);
13
14    if (room.isEmpty()) {
15      throw new Exception("Room number: " + roomNumber + " does not exist");
16    } else {
17      if (room.get().isBooked()) {
18        throw new Exception("Room already booked!");
19      } else {
20        Room updateRoomBooking = room.get();
21        updateRoomBooking.setBooked(true);
22        hotelDao.update(updateRoomBooking);
23      }
24    }
25  }
26
27  public void cancelRoomBooking(int roomNumber) throws Exception {
28
29    Optional<Room> room = hotelDao.getById(roomNumber);
30
31    if (room.isEmpty()) {
32      throw new Exception("Room number: " + roomNumber + " does not exist");
33    } else {
34      if (room.get().isBooked()) {
35        Room updateRoomBooking = room.get();
36        updateRoomBooking.setBooked(false);
37        int refundAmount = updateRoomBooking.getPrice();
38        hotelDao.update(updateRoomBooking);
39
40        LOGGER.info("Booking cancelled for room number: " + roomNumber);
41        LOGGER.info(refundAmount + " is refunded");
42      } else {
43        throw new Exception("No booking for the room exists");
44      }
45    }
46  }
47}

The Hotel class has two methods, one for booking and cancelling a room respectively. Each one of them handles a single transaction in the system, making Hotel implement the Transaction Script pattern.

The bookRoom method consolidates all the needed steps like checking if the room is already booked or not, if not booked then books the room and updates the database by using the DAO.

The cancelRoom method consolidates steps like checking if the room is booked or not, if booked then calculates the refund amount and updates the database using the DAO.

Class diagram

alt text

Applicability

Use the Transaction Script pattern when the application has only a small amount of logic and that logic won’t be extended in the future.

Consequences

  • As the business logic gets more complicated, it gets progressively harder to keep the transaction script in a well-designed state.
  • Code duplication between transaction scripts can occur.
  • Normally not easy to refactor transactions script to other domain logic patterns.
  • Domain Model
  • Table Module
  • Service Layer

Credits