标签:
class ResultBean{ private Method1Result method1Result; private Method2Result method2Result; public void setMethod1Result(Method1Result method1Result){ this.method1Result = method1Result; } public void setMethod2Result(Method2Result method2Result){ this.method2Result = method2Result; } } class Test{ public ResultBean execute1(){ ResultBean resultBean = new ResultBean(); fillMethod1Result(resultBean); fillMethod2Result(resultBean); return resultBean; } public ResultBean execute2(){ ResultBean resultBean = new ResultBean(); resultBean.setMethod1Result(makeMethod1Result()); resultBean.setMethod2Result(makeMethod2Result()); return resultBean; } public void fillMethod1Result(ResultBean resultBean){ resultBean.setMethod1Result(method1Result); } public void fillMethod2Result(ResultBean resultBean){ resultBean.setMethod2Result(method2Result); } public Method1Result makeMethod1Result(){ return method1Result; } public Method2Result makeMethod2Result(){ return method2Result; } }
如上代码中,execute1与execute2 哪个编码习惯更好呢?
个人认为execute2方法是一个好的习惯,就像我们做事情一样,领导吩咐去你去做一件事情,是希望你做完后有一个结果反馈的,所以建议在实际编码中一些业务方法如果能抽象出一个结果对象,最好抽象出来,这样增加代码可读性,同时也可以达到复用的效果。
标签:
原文地址:http://my.oschina.net/njzjf/blog/524983