【发布时间】:2015-11-10 02:17:59
【问题描述】:
我正在为以下场景寻找一个好的设计模式,
我需要按照以下步骤操作,在每个步骤之后我需要更新数据库中的状态。为此,我在该 updateStatus(String status) 中创建了一个单独的类 UpdateStatusDAO 将更新数据库中的详细信息。步骤是,
1. Update Status as Processing Started
2. Read File
3. Update Status as Reading Completed
4. Process File
5. Update Status as File Processing Completed
6. Rename File
7. Update Status as File Rename Completed
8. Update Status as Processing Completed
try{
UpdateStatusDAO updateStatusDAO = new UpdateStatusDAO();
updateStatusDAO.updateStatus("Process Started");
// Read File
try{
} catch(Exception e){
throw new Exception();
} finally{
}
updateStatusDAO.updateStatus("Reading Completed");
// Process File
try{
} catch(Exception e){
throw new Exception();
} finally{
}
updateStatusDAO.updateStatus("File Processing Completed");
// Rename File
try{
} catch(Exception e){
throw new Exception();
} finally{
}
updateStatusDAO.updateStatus("File Rename Completed");
updateStatusDAO.updateStatus("Processing Completed");
} catch(Exception e){
} finally{
}
问题是,假设由于某种原因文件处理失败。然后我需要将状态更新为阅读完成。未完成文件处理。我添加如下,
- 如果 ReadFile / Process File 发生任何异常,我将抛出异常,则不会发生进一步的更新。
有没有其他更好的方法来处理这种情况。 ?
【问题讨论】:
-
可能是一个
Proxy类,用于包装您的文件操作并执行状态更新:sourcemaking.com/design_patterns/proxy
标签: java design-patterns