面向方面 vs 面向對象 2(應用程序中需要面向哪些方面) – JAVA編程語言程序開發技術文章

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的價值至關重要.

操作所需的資源

  1. public static Set findAllBusinessUnits() throws RepositoryException {
  2. Set businessUnits = new HashSet();
  3. try {
  4. FileReader businessUnitFile = null;
  5. BufferedReader bufferedBusinessUnitFile = null;
  6. try {
  7. businessUnitFile = new FileReader(FILE_NAME);
  8. bufferedBusinessUnitFile = new BufferedReader(businessUnitFile);
  9. String businessUnitRecord;
  10. while((businessUnitRecord = bufferedBusinessUnitFile.readLine()) != null) {
  11. BusinessUnit businessUnit = BusinessUnitFactory.createBusinessUnit(businessUnitRecord);
  12. businessUnits.add(businessUnit);
  13. }
  14. finally {
  15. if(bufferedBusinessUnitFile != null) {
  16. bufferedBusinessUnitFile.close();
  17. }
  18. if(businessUnitFile != null) {
  19. businessUnitFile.close();
  20. }
  21. }
  22. catch(IOException ioe) {
  23. String message = “IOError. Unable to find Business Unit records”;
  24. logger.log(SEVERE, message, ioe);
  25. throw new RepositoryException(message, ioe);
  26. }
  27. logger.log(INFO, “Manager Records returned:” + businessUnits.size());
  28. return businessUnits;
  29. }


上面的代碼通過FileReader和BUfferedReader來讀取CSV文件中的業務數據.
應用程序重復地從資源文件中取得數據然後在操作完成後釋放.去掉程序的這個方面將提高代碼的可讀性並達到一個更好的設計,因為一旦這個方面被刪除掉,剩下的代碼將隻作它本該做的事情.在這個例子中方法的作用是讀取業務單位數據.所以不需要擔心獲取和釋放必要的資源.同樣地,使用AOP處理異常也變得不同.(後面將詳細介紹)

持久層
傳統的OOP使用倉庫類(repository classes)來打理應用程序的持久層.下面的例子說明瞭這一概念: 

  1. public class EmployeeRepository {
  2. public static void createEmployee(Employee employee) throws RepositoryException {
  3. //使用print writer把數據放入csv文件
  4. }
  5. public static String findEmployeeRecordById(String id) throws RepositoryException {
  6. //使用file reader來獲得指定id的員工數據
  7. }
  8. public static Employee findEmployeeById(String id) throws RepositoryException {
  9. //使用該方法獲取員工數據,Employee對象由工廠類創建
  10. }
  11. public static void updateEmployee(Employee employee) {
  12. //更新員工數據
  13. }
  14. }


類EmployeeService 使用一個倉庫類給應用中相關雇員提供服務,在一個企業應用中,從域模型(domain model)中去掉持久層代碼是一種設計上的改進.模型設計者和程序員就可以關註各自的業務邏輯和持久層處理.後面你將會看到如何通過AOP來達到這樣的效果.

日志
刪除用於調試和審核的日志代碼將會極大地改進代碼的可讀性.考慮下面的代碼片斷:

  1. public Employee createEmployee(String name,
  2. String contactNumber,
  3. BusinessUnit businessUnit,
  4. Manager manager)
  5. throws EmployeeServiceException {
  6. String id = createNewEmployeeId();
  7. Employee employee =
  8. EmployeeFactory.createEmployee(id, name, contactNumber, businessUnit, manager);
  9. try {
  10. EmployeeRepository.createEmployee(employee);
  11. catch(RepositoryException re) {
  12. String message = “Created employee successfully:” + employee;
  13. logger.log(SEVERE, message);
  14. throw new EmployeeServiceException(message, re);
  15. }
  16. logger.log(INFO, “Created employee successfully:” + employee);
  17. return employee;
  18. }


上面的代碼裡包含瞭一個致命錯誤和一個成功信息.日志這個方面可以移到業務模型外獨立實現.

錯誤處理
例子程序把錯誤處理這個方面留給瞭你,但這節通過上面的代碼已經討論瞭這一潛在問題.當你調用EmployeeRepository 對象的createEmployee 方法時,你可能會得到一個RepositoryException異常.傳統的解決方法是,在相同的類中處理這個錯誤.另一種選擇是,當RepositoryException 異常被拋出時createEmployee 方法返回null,catch塊中的其他邏輯可以在類外處理這一錯誤.
錯誤處理在不同的情況中也會不同.但是,通過AOP可以區分開每種情況.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *