Spring模板模式和回调接口

来源:互联网 发布:武汉群光有mac的专柜吗 编辑:程序博客网 时间:2024/05/22 13:12
 

参考Spring模板模式和回调接口的一个应用



1.假设有如下的业务方法类:

[java] view plaincopy
  1. import java.util.Map;  
  2. /** 
  3.  * 业务方法 
  4.  * @author yaol 
  5.  * 
  6.  */  
  7. public class TestService {  
  8.    BaseDao dao = new BaseDao();  
  9.    public Object findById(Map params){  
  10.        /** 
  11.         * 检测参数 
  12.         */  
  13.        Object obj =null;  
  14.        String id = null;  
  15.        try{  
  16.            id = (String)params.get("id");  
  17.        }  
  18.        catch(Exception e){  
  19.            e.printStackTrace();  
  20.            return error();  
  21.        }  
  22.        /** 
  23.         * 获取结果 
  24.         */  
  25.        try{  
  26.            obj = dao.findById(id);  
  27.            return ok();  
  28.        }  
  29.        catch(Exception e){  
  30.            e.fillInStackTrace();  
  31.            return error();  
  32.        }  
  33.    }  
  34.      
  35.    public Object findByPage(Map params){  
  36.          
  37.        Object obj =null;  
  38.        int pageNo,pageSize;  
  39.        /** 
  40.         * 检测参数 
  41.         */  
  42.        try{  
  43.            pageNo =(Integer) params.get("pageNo");  
  44.            pageSize = (Integer)params.get("pageSize");  
  45.        }  
  46.        catch(Exception e){  
  47.            e.printStackTrace();  
  48.            return error();  
  49.        }  
  50.        /** 
  51.         * 获取结果 
  52.         */  
  53.        try{  
  54.            obj = dao.findByPage(pageNo, pageSize);  
  55.            return ok();  
  56.        }  
  57.        catch(Exception e){  
  58.            e.fillInStackTrace();  
  59.            return error();  
  60.        }  
  61.    }  
  62.      
  63.    public Object error(){  
  64.        return "error";  
  65.    }  
  66.    public String ok(){  
  67.        return "ok";  
  68.    }  
  69. }  
  70. /** 
  71.  * Dao 
  72.  * @author yaol 
  73.  * 
  74.  */  
  75. class BaseDao{  
  76.     public Object findById(String id){return null;}  
  77.     public Object findByPage(int pageNo,int pageSize){return null;}  
  78. }  


应该可以看出findById和findByPage结构是完全相同的.
先校验参数合法性,再根据参数获取结果,并且分别做异常处理.
如果业务方法增加的话,程序中会充斥了大量的异常处理代码.
如何减少这些重复的异常处理代码呢?

2.引入模板模式和回调接口

代码如下:

[java] view plaincopy
  1. /** 
  2.  * 回调接口 
  3.  * @author yaol 
  4.  * 
  5.  */  
  6. interface ServiceCallback {  
  7.     /** 
  8.      * 验证参数合法性以及设置参数 
  9.      * @param map 
  10.      */  
  11.     public void checkAndSetParamters();  
  12.       
  13.     /** 
  14.      * 获取数据 
  15.      * @param map 
  16.      * @return TODO 
  17.      */  
  18.     public Object getResult() throws Exception;  
  19. }  
  20. /** 
  21.  * 业务模板 
  22.  * @author yaol 
  23.  * 
  24.  */  
  25. public class ServiceTemplate {  
  26.       
  27.     public Object execute(ServiceCallback callback) {  
  28.         try {  
  29.             callback.checkAndSetParamters();  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.             return error();  
  33.         }  
  34.         try {  
  35.             Object obj = callback.getResult();  
  36.             return ok();  
  37.         } catch (Exception e) {  
  38.             e.fillInStackTrace();  
  39.             return error();  
  40.         }  
  41.     }  
  42.   
  43.     public Object error() {  
  44.         return "error";  
  45.     }  
  46.   
  47.     public String ok() {  
  48.         return "ok";  
  49.     }  
  50. }  


3.改写业务方法类

[java] view plaincopy
  1. /** 
  2.  * 改写后的业务方法类 
  3.  * @author yaol 
  4.  * 
  5.  */  
  6. public class TestService2 extends ServiceSupport{  
  7.     public Object findByPage(final Map params) {  
  8.         return getServiceTemplate().execute(new ServiceCallback() {  
  9.             int pageNo, pageSize;  
  10.             public void checkAndSetParamters() {  
  11.                 pageNo = (Integer) params.get("pageNo");  
  12.                 pageSize = (Integer) params.get("pageSize");  
  13.             }  
  14.             public Object getResult() throws Exception {  
  15.                 return getDao().findByPage(pageNo, pageSize);  
  16.             }  
  17.         });  
  18.     }  
  19.   
  20.     public Object findById(final Map params) {  
  21.         return getServiceTemplate().execute(new ServiceCallback() {  
  22.             String id = null;  
  23.             public void checkAndSetParamters() {  
  24.                 id = (String)params.get("id");  
  25.             }  
  26.             public Object getResult() throws Exception {  
  27.                 return getDao().findById(id);  
  28.             }  
  29.         });  
  30.     }  
  31.     public BaseDao getDao() {  
  32.         return new BaseDao();  
  33.     }  
  34. }  
  35. /** 
  36.  * ServiceSupport 
  37.  * @author yaol 
  38.  * 
  39.  */  
  40. class ServiceSupport{  
  41.     public ServiceTemplate getServiceTemplate() {  
  42.         return new ServiceTemplate();  
  43.     }  
  44. }  
[java] view plaincopy
  1. /* 
  2. 这里的ServiceSupport和Spring提供的XXXDaoSupport相似,而getServiceTemplate()类似于getHibernateTemplate()方法. 
  3. 阅读优秀的开源代码对程序员是大有裨益的. 
0 0
原创粉丝点击