论坛首页 入门技术论坛

这段代码怎样重构?

浏览 6034 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-02-27  
DispatcherAction费那么大劲把业务集成在一个类里 这么做的话不是又分出来了么 代码量是不是增加的不少
0 请登录后投票
   发表时间:2007-02-27  
intolong 写道
xly_971223 写道
intolong 写道
huangpengxiao 写道
    如何封装变化 楼上继续


写完了啊,其实方案二就是lz代码的语法糖


方法二正是我要找的方法


方法1更加职责分明。


呵呵 晓得。我只是想看看不用filter或aop怎么处理这种情况

0 请登录后投票
   发表时间:2007-02-27  
不错不错啊 !方法一 用 SPRING ???
0 请登录后投票
   发表时间:2007-02-27  
huangpengxiao 写道
DispatcherAction费那么大劲把业务集成在一个类里 这么做的话不是又分出来了么 代码量是不是增加的不少


DispatcherAction也不是什么好东西。

BTW,说了方法2就是个语法糖。
0 请登录后投票
   发表时间:2007-02-27  
intolong 写道
huangpengxiao 写道
DispatcherAction费那么大劲把业务集成在一个类里 这么做的话不是又分出来了么 代码量是不是增加的不少


DispatcherAction也不是什么好东西。

BTW,说了方法2就是个语法糖。


哦~~~~! 倒也是
0 请登录后投票
   发表时间:2007-02-27  
这种东西有什么重构不重构的,两个选择嘛,你要不就是filter,要不就是在baseaction中的execute方法里面做,考虑到其他方法可能不要登陆,那你就把baseaction搞成两个,一个是要登陆的,一个是不要登陆的,更或者把两个方法结合起来,在filter里设标志位,在baseaction里的execute方法中判断一下要不要调用登陆的方法就可以了。重构这顶帽子还扣不到这点代码上去吧
0 请登录后投票
   发表时间:2007-03-13  
interface ActionProcessor{   
        ActionForward process(      
                ActionMapping mapping,      
                ActionForm form,      
                HttpServletRequest request,      
                HttpServletResponse response) ;   
}   
class CreateOrder implements  ActionProcessor{
     ActionForward process(      
                ActionMapping mapping,      
                ActionForm form,      
                HttpServletRequest request,      
                HttpServletResponse response){
        CustomOrder order = new CustomOrder();   
                   //省略order.setXXX();   
        OrderManager orderManager = getOrderManager();   
        try {   
            orderManager.createOrder(order);   
        } catch (OrderException e) {   
        }   
           
        return mapping.findForward("saveSuccess");
     } 
}   
class FindOrderByCustomId implements  ActionProcessor{
     ActionForward process(      
                ActionMapping mapping,      
                ActionForm form,      
                HttpServletRequest request,      
                HttpServletResponse response){
                OrderManager orderManager = getOrderManager();   
        List orders = orderManager.findOrderByCustomId(loginUser.getCustomId());   
           
        request.setAttribute("orders", orders);   
        return mapping.findForward("list"); 
     } 
}
abstract class AbstractAction extends BaseAction{   
        protected final ActionForward processWithLogin(      
                ActionMapping mapping,      
                ActionForm form,      
                HttpServletRequest request,      
                HttpServletResponse response,   
                ActionProcessor processor) {    
                LoginUser loginUser = getLoginUser(request);      
                if( !loginUser.isLogin() ){      
                    //go to login      
                }    
                return processor.process(mapping, form, request, response);   
        }   
}   
  
public class OrderManagerActions extends AbstractAction {      
    /**     
     * 创建新订单    
     */     
    public ActionForward create(      
        ActionMapping mapping,      
        ActionForm form,      
        HttpServletRequest request,      
        HttpServletResponse response) {   
CreateOrder createOrder = new CreateOrder();   
        return processWithLogin(mapping,form,request,response,createOrder);      
    }      
          
    /**    
     * 查看订单    
     */     
    public ActionForward list(      
            ActionMapping mapping,      
            ActionForm form,      
            HttpServletRequest request,      
            HttpServletResponse response) { 
        FindOrderByCustomId findOrderByCustomId = new FindOrderByCustomId();     
        return processWithLogin(mapping,form,request,response,findOrderByCustomId );    
    }      
}    

[size=9][/size]
似乎是用到了一个什么模式,但是这样好么?
0 请登录后投票
   发表时间:2007-03-13  
其实还是AOP或者fliter做起来比较方便一些
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics