当前位置:编程学习 > JAVA >>

面向方面 vs 面向对象 2(应用程序中需要面向哪些方面)

by Narayanan A.R. June 15, 2005
翻译zhangv (derekzhangv.at.hotmail.com)
原文: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可以区分开每种情况.
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,