Is refactor method to field in this case?

原创 2016年12月22日 11:13:00
public class WebContext(){

    private HttpServletRequest request;

    private WebContext(){};
    public static WebContext newPrototype(HttpServletRequest request){
        WebContext wc = new WebContext();
        wc.request = request;
    }

    public boolean isLogin(){
        return request.getSession().getAttribute(CONSTANT.SESSION_TOKEN)==null;
    }

    private RemoteUser getRemoteUser(){
        RemoteUser ru = new RemoteUser();
        if(isLogin()){
            ru = ...;    //...get remote user info from remote server
        }
        return ru;
    }

    public User getUser(){
        User user = new User();
        if(isLogin()){
            RemoteUser ru = getRemoteUser();
            long userid = ru.getUserid();
            user = ...;  //... get user info from db by userid;
        }
        return user;
    }

    public Trader getTrader(){
        Trader trader = new Trader();
        if(isLogin()){
            RemoteUser ru = getRemoteUser();
            long traderid = ru.getTraderid();
            trader = ...;  //... get trader info from db by traderid;
        }
        return trader;
    }
}

this make me uncomfortable, getUser() and getTrader() all must invoke getRemoteUser() firstly. so i refactor this class to

public class WebContext(){

    private HttpServletRequest request;
    private boolean isLogin;
    private RemoteUser ru;

    private WebContext(){};
    public static WebContext newPrototype(HttpServletRequest request){
        WebContext wc = new WebContext();
        wc.request = request;
        wc.islogin = request.getSession().getAttribute(CONSTANT.SESSION_TOKEN)==null;
        wc.ru = getRemoteUser();
    }

    public boolean isLogin(){
        return isLogin;
    }

    private RemoteUser getRemoteUser(){
        RemoteUser ru = new RemoteUser();
        if(isLogin){
            ru = ...;    //...get remote user info from remote server
        }
        return ru;
    }

    public User getUser(){
        User user = new User();
        if(isLogin){
            long userid = ru.getUserid();
            user = ...;  //... get user info from db by userid;
        }
        return user;
    }

    public Trader getTrader(){
        Trader trader = new Trader();
        if(isLogin){
            long traderid = ru.getTraderid();
            trader = ...;  //... get trader info from db by traderid;
        }
        return trader;
    }
}

refactored construct method init RemoteUser once. but i am confident about refactored construct method is not good for reused.i am confused how should i do for good code design;

参考资料

stack overflow跳点

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

This jewelry is also in comparison to a rose

"It attracts a lot of interest," Matthews laughed about all of his gear, even though trodding along ...

Name [spring.liveBeansView.mbeanDomain] is not bound in this Context. Unable to find

修改项目的配置文件,log4j的日志输出级别修改为DEBUG,打印详细的错误信息后,启动项目控制台输出有以下异常信息:[org.springframework.jndi.JndiTemplate]-L...

iOS--错误集锦--Capturing 'self' strongly in this block is likely to lead to a retain cycle

申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨 问题 这个问题实际上就是代码块循环引用问题。 解决的方法有两种,一种是这样子的写法:把MYQRCodeView写为__weak【...

proguard 提示Warning: can't find referenced field/method '...' in library class ...等问题

最近用到maven+proguard来做java代码混淆,在添加完pom.xml以及配置文件proguard.conf后遇到了三个问题。 1.Warning: can't find reference...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)