#1321 Added comments about TS
This commit is contained in:
parent
75b10ed657
commit
e99bcf772b
@ -29,6 +29,20 @@ import org.h2.jdbcx.JdbcDataSource;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transaction Script (TS) is one of the simplest domain logic pattern.
|
||||||
|
* It needs less work to implement than other domain logic patterns and therefore
|
||||||
|
* it’s perfect fit for smaller applications that don't need big architecture behind them.
|
||||||
|
*
|
||||||
|
* <p>In this example we will use the TS pattern to implement booking and cancellation
|
||||||
|
* methods for a Hotel management App. The main method will initialise an instance of
|
||||||
|
* {@link Hotel} and add rooms to it. After that it will book and cancel a couple of rooms
|
||||||
|
* and that will be printed by the logger.</p>
|
||||||
|
*
|
||||||
|
* <p>The thing we have to note here is that all the operations related to booking or cancelling
|
||||||
|
* a room like checking the database if the room exists, checking the booking status or the
|
||||||
|
* room, calculating refund price are all clubbed inside a single transaction script method.</p>
|
||||||
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final String H2_DB_URL = "jdbc:h2:~/test";
|
private static final String H2_DB_URL = "jdbc:h2:~/test";
|
||||||
@ -36,7 +50,8 @@ public class App {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
* Initialises an instance of Hotel and adds rooms to it.
|
||||||
|
* Carries out booking and cancel booking transactions.
|
||||||
* @param args command line arguments
|
* @param args command line arguments
|
||||||
* @throws Exception if any error occurs
|
* @throws Exception if any error occurs
|
||||||
*/
|
*/
|
||||||
@ -47,12 +62,15 @@ public class App {
|
|||||||
createSchema(dataSource);
|
createSchema(dataSource);
|
||||||
final var dao = new HotelDaoImpl(dataSource);
|
final var dao = new HotelDaoImpl(dataSource);
|
||||||
|
|
||||||
|
// Add rooms
|
||||||
addRooms(dao);
|
addRooms(dao);
|
||||||
|
|
||||||
|
// Print room booking status
|
||||||
getRoomStatus(dao);
|
getRoomStatus(dao);
|
||||||
|
|
||||||
var hotel = new Hotel(dao);
|
var hotel = new Hotel(dao);
|
||||||
|
|
||||||
|
// Book rooms
|
||||||
hotel.bookRoom(1);
|
hotel.bookRoom(1);
|
||||||
hotel.bookRoom(2);
|
hotel.bookRoom(2);
|
||||||
hotel.bookRoom(3);
|
hotel.bookRoom(3);
|
||||||
@ -60,6 +78,7 @@ public class App {
|
|||||||
hotel.bookRoom(5);
|
hotel.bookRoom(5);
|
||||||
hotel.bookRoom(6);
|
hotel.bookRoom(6);
|
||||||
|
|
||||||
|
// Cancel booking for a few rooms
|
||||||
hotel.cancelRoomBooking(1);
|
hotel.cancelRoomBooking(1);
|
||||||
hotel.cancelRoomBooking(3);
|
hotel.cancelRoomBooking(3);
|
||||||
hotel.cancelRoomBooking(5);
|
hotel.cancelRoomBooking(5);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user