📄 defaulteditcustomerservice.java
字号:
package com.ttdev.bank;
public class DefaultEditCustomerService implements EditCustomerService {
private SystemTransaction transaction;
private SystemTransactionFactory transactionFactory;
private CustomersSnapshotTaker snapshotTaker;
public Customer getCustomerForEdit(String customerId) {
startTransaction();
try {
Customer customer = loadCustomer(customerId);
commit();
return customer;
} catch (Exception e) {
rollback();
throw new RuntimeException(e);
}
}
public void saveCustomer(Customer updatedCustomer, Customer oldCustomer) {
startTransaction();
try {
String customerId = oldCustomer.getId();
Customer customerFromDB = loadCustomer(customerId);
if (!oldCustomer.equals(customerFromDB)) {
throw new RuntimeException("Data has changed");
}
saveCustomer(updatedCustomer);
commit();
} catch (RuntimeException e) {
rollback();
throw e;
}
}
private Customer loadCustomer(String customerId) {
Customers customers = snapshotTaker.getCustomersSnapshot(transaction);
return customers.loadCustomer(customerId);
}
private void startTransaction() {
transaction = transactionFactory.start();
}
private void commit() {
transaction.commit();
}
private void rollback() {
transaction.rollback();
}
private void saveCustomer(Customer customer) {
Customers customers = snapshotTaker.getCustomersSnapshot(transaction);
customers.saveCustomer(customer);
}
public void setTransactionFactory(SystemTransactionFactory transactionFactory) {
this.transactionFactory = transactionFactory;
}
public void setSnapshotTaker(CustomersSnapshotTaker snapshotTaker) {
this.snapshotTaker = snapshotTaker;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -