mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 12:59:00 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -31,18 +31,18 @@ import org.slf4j.Logger;
|
||||
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.
|
||||
* 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>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>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>
|
||||
* <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.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@@ -50,9 +50,9 @@ public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
* Initialises an instance of Hotel and adds rooms to it.
|
||||
* Carries out booking and cancel booking transactions.
|
||||
* 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
|
||||
* @throws Exception if any error occurs
|
||||
*/
|
||||
@@ -87,7 +87,6 @@ public class App {
|
||||
getRoomStatus(dao);
|
||||
|
||||
deleteSchema(dataSource);
|
||||
|
||||
}
|
||||
|
||||
private static void getRoomStatus(HotelDaoImpl dao) throws Exception {
|
||||
@@ -98,14 +97,14 @@ public class App {
|
||||
|
||||
private static void deleteSchema(DataSource dataSource) throws java.sql.SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws Exception {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL);
|
||||
} catch (Exception e) {
|
||||
throw new Exception(e.getMessage(), e);
|
||||
|
||||
@@ -26,9 +26,7 @@ package com.iluwatar.transactionscript;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Hotel class to implement TS pattern.
|
||||
*/
|
||||
/** Hotel class to implement TS pattern. */
|
||||
@Slf4j
|
||||
public class Hotel {
|
||||
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.transactionscript;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* DAO interface for hotel transactions.
|
||||
*/
|
||||
/** DAO interface for hotel transactions. */
|
||||
public interface HotelDao {
|
||||
|
||||
Stream<Room> getAll() throws Exception;
|
||||
|
||||
@@ -36,9 +36,7 @@ import java.util.stream.StreamSupport;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Implementation of database operations for Hotel class.
|
||||
*/
|
||||
/** Implementation of database operations for Hotel class. */
|
||||
@Slf4j
|
||||
public class HotelDaoImpl implements HotelDao {
|
||||
|
||||
@@ -54,28 +52,31 @@ public class HotelDaoImpl implements HotelDao {
|
||||
var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM ROOMS"); // NOSONAR
|
||||
var resultSet = statement.executeQuery(); // NOSONAR
|
||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE,
|
||||
Spliterator.ORDERED) {
|
||||
return StreamSupport.stream(
|
||||
new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE, Spliterator.ORDERED) {
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Room> action) {
|
||||
try {
|
||||
if (!resultSet.next()) {
|
||||
return false;
|
||||
}
|
||||
action.accept(createRoom(resultSet));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e); // NOSONAR
|
||||
}
|
||||
}
|
||||
}, false).onClose(() -> {
|
||||
try {
|
||||
mutedClose(connection, statement, resultSet);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Room> action) {
|
||||
try {
|
||||
if (!resultSet.next()) {
|
||||
return false;
|
||||
}
|
||||
action.accept(createRoom(resultSet));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e); // NOSONAR
|
||||
}
|
||||
}
|
||||
},
|
||||
false)
|
||||
.onClose(
|
||||
() -> {
|
||||
try {
|
||||
mutedClose(connection, statement, resultSet);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new Exception(e.getMessage(), e);
|
||||
}
|
||||
@@ -86,7 +87,7 @@ public class HotelDaoImpl implements HotelDao {
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM ROOMS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("SELECT * FROM ROOMS WHERE ID = ?")) {
|
||||
|
||||
statement.setInt(1, id);
|
||||
resultSet = statement.executeQuery();
|
||||
@@ -111,7 +112,7 @@ public class HotelDaoImpl implements HotelDao {
|
||||
}
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("INSERT INTO ROOMS VALUES (?,?,?,?)")) {
|
||||
var statement = connection.prepareStatement("INSERT INTO ROOMS VALUES (?,?,?,?)")) {
|
||||
statement.setInt(1, room.getId());
|
||||
statement.setString(2, room.getRoomType());
|
||||
statement.setInt(3, room.getPrice());
|
||||
@@ -126,10 +127,9 @@ public class HotelDaoImpl implements HotelDao {
|
||||
@Override
|
||||
public Boolean update(Room room) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement =
|
||||
connection
|
||||
.prepareStatement("UPDATE ROOMS SET ROOM_TYPE = ?, PRICE = ?, BOOKED = ?"
|
||||
+ " WHERE ID = ?")) {
|
||||
var statement =
|
||||
connection.prepareStatement(
|
||||
"UPDATE ROOMS SET ROOM_TYPE = ?, PRICE = ?, BOOKED = ?" + " WHERE ID = ?")) {
|
||||
statement.setString(1, room.getRoomType());
|
||||
statement.setInt(2, room.getPrice());
|
||||
statement.setBoolean(3, room.isBooked());
|
||||
@@ -143,7 +143,7 @@ public class HotelDaoImpl implements HotelDao {
|
||||
@Override
|
||||
public Boolean delete(Room room) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("DELETE FROM ROOMS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("DELETE FROM ROOMS WHERE ID = ?")) {
|
||||
statement.setInt(1, room.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (Exception e) {
|
||||
@@ -167,7 +167,8 @@ public class HotelDaoImpl implements HotelDao {
|
||||
}
|
||||
|
||||
private Room createRoom(ResultSet resultSet) throws Exception {
|
||||
return new Room(resultSet.getInt("ID"),
|
||||
return new Room(
|
||||
resultSet.getInt("ID"),
|
||||
resultSet.getString("ROOM_TYPE"),
|
||||
resultSet.getInt("PRICE"),
|
||||
resultSet.getBoolean("BOOKED"));
|
||||
|
||||
@@ -30,9 +30,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* A room POJO that represents the data that will be read from the data source.
|
||||
*/
|
||||
/** A room POJO that represents the data that will be read from the data source. */
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@@ -44,5 +42,4 @@ public class Room {
|
||||
private String roomType;
|
||||
private int price;
|
||||
private boolean booked;
|
||||
|
||||
}
|
||||
|
||||
@@ -24,16 +24,12 @@
|
||||
*/
|
||||
package com.iluwatar.transactionscript;
|
||||
|
||||
/**
|
||||
* Customer Schema SQL Class.
|
||||
*/
|
||||
/** Customer Schema SQL Class. */
|
||||
public final class RoomSchemaSql {
|
||||
|
||||
public static final String CREATE_SCHEMA_SQL =
|
||||
"CREATE TABLE ROOMS (ID NUMBER, ROOM_TYPE VARCHAR(100), PRICE INT, BOOKED VARCHAR(100))";
|
||||
public static final String DELETE_SCHEMA_SQL = "DROP TABLE ROOMS IF EXISTS";
|
||||
|
||||
private RoomSchemaSql() {
|
||||
}
|
||||
|
||||
private RoomSchemaSql() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user