by Narayanan A.R. June 15, 2005
翻譯zhangv (derekzhangv.at.hotmail.com)
原文:Java/Article/28422/0/page/2″>http://www.devx.com/Java/Article/28422/0/page/2
應用程序中需要面向哪些”方面”
到目前為止,對模型和設計的討論還限於一個較抽象的層面.現在,我轉向這個應用其他方面 – 這對理解AOP的價值至關重要.
操作所需的資源
- public static Set findAllBusinessUnits() throws RepositoryException {
- Set businessUnits = new HashSet();
- try {
- FileReader businessUnitFile = null;
- BufferedReader bufferedBusinessUnitFile = null;
- try {
- businessUnitFile = new FileReader(FILE_NAME);
- bufferedBusinessUnitFile = new BufferedReader(businessUnitFile);
- String businessUnitRecord;
- while((businessUnitRecord = bufferedBusinessUnitFile.readLine()) != null) {
- BusinessUnit businessUnit = BusinessUnitFactory.createBusinessUnit(businessUnitRecord);
- businessUnits.add(businessUnit);
- }
- } finally {
- if(bufferedBusinessUnitFile != null) {
- bufferedBusinessUnitFile.close();
- }
- if(businessUnitFile != null) {
- businessUnitFile.close();
- }
- }
- } catch(IOException ioe) {
- String message = “IOError. Unable to find Business Unit records”;
- logger.log(SEVERE, message, ioe);
- throw new RepositoryException(message, ioe);
- }
- logger.log(INFO, “Manager Records returned:” + businessUnits.size());
- return businessUnits;
- }
上面的代碼通過FileReader和BUfferedReader來讀取CSV文件中的業務數據.
應用程序重復地從資源文件中取得數據然後在操作完成後釋放.去掉程序的這個方面將提高代碼的可讀性並達到一個更好的設計,因為一旦這個方面被刪除掉,剩下的代碼將隻作它本該做的事情.在這個例子中方法的作用是讀取業務單位數據.所以不需要擔心獲取和釋放必要的資源.同樣地,使用AOP處理異常也變得不同.(後面將詳細介紹)
持久層
傳統的OOP使用倉庫類(repository classes)來打理應用程序的持久層.下面的例子說明瞭這一概念:
- public class EmployeeRepository {
- public static void createEmployee(Employee employee) throws RepositoryException {
- //使用print writer把數據放入csv文件
- }
- public static String findEmployeeRecordById(String id) throws RepositoryException {
- //使用file reader來獲得指定id的員工數據
- }
- public static Employee findEmployeeById(String id) throws RepositoryException {
- //使用該方法獲取員工數據,Employee對象由工廠類創建
- }
- public static void updateEmployee(Employee employee) {
- //更新員工數據
- }
- }
類EmployeeService 使用一個倉庫類給應用中相關雇員提供服務,在一個企業應用中,從域模型(domain model)中去掉持久層代碼是一種設計上的改進.模型設計者和程序員就可以關註各自的業務邏輯和持久層處理.後面你將會看到如何通過AOP來達到這樣的效果.
日志
刪除用於調試和審核的日志代碼將會極大地改進代碼的可讀性.考慮下面的代碼片斷:
- public Employee createEmployee(String name,
- String contactNumber,
- BusinessUnit businessUnit,
- Manager manager)
- throws EmployeeServiceException {
- String id = createNewEmployeeId();
- Employee employee =
- EmployeeFactory.createEmployee(id, name, contactNumber, businessUnit, manager);
- try {
- EmployeeRepository.createEmployee(employee);
- } catch(RepositoryException re) {
- String message = “Created employee successfully:” + employee;
- logger.log(SEVERE, message);
- throw new EmployeeServiceException(message, re);
- }
- logger.log(INFO, “Created employee successfully:” + employee);
- return employee;
- }
上面的代碼裡包含瞭一個致命錯誤和一個成功信息.日志這個方面可以移到業務模型外獨立實現.
錯誤處理
例子程序把錯誤處理這個方面留給瞭你,但這節通過上面的代碼已經討論瞭這一潛在問題.當你調用EmployeeRepository 對象的createEmployee 方法時,你可能會得到一個RepositoryException異常.傳統的解決方法是,在相同的類中處理這個錯誤.另一種選擇是,當RepositoryException 異常被拋出時createEmployee 方法返回null,catch塊中的其他邏輯可以在類外處理這一錯誤.
錯誤處理在不同的情況中也會不同.但是,通過AOP可以區分開每種情況.