提交 fa68789c 编写于 作者: A Ashish Trivedi

#1321 Updated readme and add UML diagram

上级 12d39293
...@@ -11,33 +11,110 @@ tags: ...@@ -11,33 +11,110 @@ tags:
## Intent ## Intent
Transaction script(TS) is mainly used in small applications where nothing complex is done and bigger architecture's are not needed. Transaction script(TS) is mainly used in small applications where nothing complex is done and bigger architecture's are not needed.
## Name / classification ## Explanation
... Real world example
> Your need is to be able to book a hotel room and also be able to cancel that booking.
>
## Also known as In plain words
... > All logic related to booking a hotel room like checking room availability,
> calculate rates and update the database is done inside a single transaction script.
> Similar procedure is also needed for cancelling a room booking and all
> that logic will be in another transaction script.
Programmatic example
## Explanation The Hotel class takes care of booking and cancelling a room in a hotel.
...
```java
public class Hotel {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionScriptApp.class);
private 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");
}
}
}
}
```
This class has two methods, one for booking and cancelling a room respectively.
```
public void bookRoom(int roomNumber);
```
The book room method consolidates all the needed steps like checking if the room is already booked
or not, if not booked then books the rooma nd updates the database by using the DAO.
```
public void cancelRoomBooking(int roomNumber)
```
The cancel room 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 ## Class diagram
... ![alt text](./etc/transaction-script.png "Transaction script model")
## Applicability ## Applicability
... Use the transaction script model when the application has only a small amount of logic and that
logic won't be extended in the future.
## Tutorials
...
## Known uses ## Known uses
... * Revenue recognition in business systems.
## Consequences (the good and the bad, add criticism here) ## 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.
## Related patterns ## Related patterns
... * Domain Model
* Table Module
* Service Layer
## Credits ## Credits
... * [Transaction Script Pattern](https://dzone.com/articles/transaction-script-pattern#:~:text=Transaction%20Script%20(TS)%20is%20the,need%20big%20architecture%20behind%20them.)
* [Transaction Script](https://www.informit.com/articles/article.aspx?p=1398617)
* [Patterns of Enterprise Application Architecture](https://www.amazon.com/gp/product/0321127420?ie=UTF8&tag=gupesasnebl-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0321127420)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册