java-design-patterns/transaction-script
Subhrodip Mohanta 462b581b34
clearing Sonar Blockers (#1633)
* update gitignore

.checkstyle files are being tracked which should not be

* NOSONAR for statement

excluded from SONAR analysis as it is already dealt using functional approach

https://sonarcloud.io/project/issues?id=iluwatar_java-design-patterns&issues=AW8FwRBhm8eoEVQR-x0f&open=AW8FwRBhm8eoEVQR-x0f

* achieved thread safety with lazy initialization

https://sonarcloud.io/project/issues?fileUuids=AXb6t0PKusn4P8Tm-LmM&id=iluwatar_java-design-patterns&open=AXb6t19yusn4P8Tm-Lmo&resolved=false

* remove double checked locking and initialize before using

https://sonarcloud.io/project/issues?fileUuids=AXb6t0PKusn4P8Tm-LmK&id=iluwatar_java-design-patterns&open=AXb6t19qusn4P8Tm-Lmk&resolved=false

* NOSONAR for the line

https://sonarcloud.io/project/issues?id=iluwatar_java-design-patterns&issues=AXPd3iSe46HRSze7cz3D&open=AXPd3iSe46HRSze7cz3D

Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2021-01-27 16:24:06 +02:00
..
2020-08-11 00:23:00 +05:30
2021-01-27 16:24:06 +02:00
2020-07-19 16:19:01 +05:30
2020-08-11 17:26:05 +03:00

layout, title, folder, permalink, categories, tags
layout title folder permalink categories tags
pattern Transaction Script transaction-script /patterns/transaction-script/ Behavioral
Data access

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.

public class Hotel {
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

  private final HotelDaoImpl hotelDao;

  public Hotel(HotelDaoImpl hotelDao) {
    this.hotelDao = hotelDao;
  }

  public void bookRoom(int roomNumber) throws Exception {

    Optional<Room> room = hotelDao.getById(roomNumber);

    if (room.isEmpty()) {
      throw new Exception("Room number: " + roomNumber + " does not exist");
    } else {
      if (room.get().isBooked()) {
        throw new Exception("Room already booked!");
      } else {
        Room updateRoomBooking = room.get();
        updateRoomBooking.setBooked(true);
        hotelDao.update(updateRoomBooking);
      }
    }
  }

  public void cancelRoomBooking(int roomNumber) throws Exception {

    Optional<Room> room = hotelDao.getById(roomNumber);

    if (room.isEmpty()) {
      throw new Exception("Room number: " + roomNumber + " does not exist");
    } else {
      if (room.get().isBooked()) {
        Room updateRoomBooking = room.get();
        updateRoomBooking.setBooked(false);
        int refundAmount = updateRoomBooking.getPrice();
        hotelDao.update(updateRoomBooking);

        LOGGER.info("Booking cancelled for room number: " + roomNumber);
        LOGGER.info(refundAmount + " is refunded");
      } else {
        throw new Exception("No booking for the room exists");
      }
    }
  }
}

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