我有这种情况,在一笔交易被视为一笔交易成功后,我只需要执行一次批准子模块。
所以在循环中,我需要检查程序是否已经执行了批准。
目前,我想到了两种方法来实现
我使用isApproved值通过用performApproveSubModule方法的返回值更新其自身值来进行检查:
List<Transaction> transactionList = request.getTransactionList();
boolean isApproved = false;
for(Transaction transaction : transactionList) {
//transaction business logic, long code
transaction.setStatus("Success");
//approve transacion sub module
isApproved = performApproveSubModule(isApproved, transaction);
}
//method
private boolean performApproveSubModule(boolean isApproved, Transaction transaction) {
if(!isApproved) {
//approve business logic
// update isApproved value to true
isApproved = true;
}
return isApproved;
}
我也使用isApproved值进行检查,但我通过使用Wrapper类将其作为更新值的引用:
AtomicBoolean isApproved = new AtomicBoolean();
for(Transaction transaction : transactionList) {
//transaction business logic, long code
transaction.setStatus("Success");
//approve transacion sub module
performApproveSubModule(isApproved, transaction);
}
//method
private void performApproveSubModule(AtomicBoolean isApproved, Transaction transaction) {
if(!isApproved.get()) {
//approve business logic
// update isApproved value to true
isApproved.set(true);
}
}
我应该使用哪一个? 我应该使用其他正确的方法吗?