你好BANQ,你觉的execute方法中的循环代码和组装respMsg对象的代码有没有必要在这个类中再加两个方法封装

06-12-31 xysniper

package com.moloon.sonny.action;

import java.util.Iterator;

import java.util.List;

import com.moloon.aslan.core.Item;

import com.moloon.aslan.core.Request;

import com.moloon.aslan.core.Response;

import com.moloon.aslan.core.support.ActionSupport;

import com.moloon.framework.exception.ServiceException;

import com.moloon.framework.page.Page;

import com.moloon.sonny.message.request.CircleAlbumInfoMessage;

import com.moloon.sonny.message.response.CircleAlbumInfoRespMessage;

import com.moloon.sonny.message.response.ErrorRespMessage;

import com.moloon.sonny.message.response.item.AlbumUserInfoRespItem;

import com.moloon.sonny.message.response.item.SimpleCircleItem;

import com.moloon.sonny.pojo.Troop;

import com.moloon.sonny.service.CircleService;

import com.moloon.sonny.service.UserBaseInfoService;

import com.moloon.sonny.util.PageCalculator;

import com.moloon.sonny.vo.PhotoListVO;

import com.moloon.sonny.vo.SmsUserBaseInfoVO;

/**

* 请求圈相册信息

*

* @author lvyg

* @version 1.1, 2006-12-7

* @since JDK1.5

*/

public class LoadCircleAlbumAction extends ActionSupport {

private CircleService circleService;

private UserBaseInfoService baseInfoService;

private int pageStep = 10;

public UserBaseInfoService getBaseInfoService() {

return baseInfoService;

}

public void setBaseInfoService(UserBaseInfoService baseInfoService) {

this.baseInfoService = baseInfoService;

}

/**

* @return Returns the pageStep.

*/

public int getPageStep() {

return pageStep;

}

/**

* @param pageStep

* The pageStep to set.

*/

public void setPageStep(int pageStep) {

this.pageStep = pageStep;

}

/**

* @return Returns the circleService.

*/

public CircleService getCircleService() {

return circleService;

}

/**

* @param circleService

* The circleService to set.

*/

public void setCircleService(CircleService circleService) {

this.circleService = circleService;

}

@Override

public String execute(Request request, Response response) {

CircleAlbumInfoMessage reqMsg = (CircleAlbumInfoMessage) request

.getMessage();

Page page = new Page();

page.setCurrPageNo(reqMsg.getPageId());

page.setPageSize(pageStep);

PageCalculator pageBar = new PageCalculator(page);

List list = null;

try {

list = circleService.getPhotoUserInfo(String.valueOf(reqMsg

.getCricleId()), page);

Item[] albumUserInfoItem = null;

if (list != null&&list.size()>0) {

albumUserInfoItem=new AlbumUserInfoRespItem[list.size()];

Iterator it = list.iterator();

int i=0;

while(it.hasNext()) {

PhotoListVO photoVo = (PhotoListVO) it.next();

AlbumUserInfoRespItem respItem = new AlbumUserInfoRespItem();

respItem.setName(photoVo.getUserName());

respItem.setUserId(photoVo.getUserId());

respItem.setAge(photoVo.getAge().byteValue());

respItem.setSex(photoVo.getSex().byteValue());

respItem.setProvName(photoVo.getProvinceName());

try {

SmsUserBaseInfoVO vo=baseInfoService.getUserBaseInfoVO(photoVo.getUserId());

respItem.setCircleOwner(vo.isIsTroopCreater());

} catch (Exception e) {

respItem.setCircleOwner(false);

}

albumUserInfoItem[i++] = respItem;

}

}

CircleAlbumInfoRespMessage respMsg = new CircleAlbumInfoRespMessage();

if(null == albumUserInfoItem || 0 == albumUserInfoItem.length) {

respMsg.setUserNum(0);

} else {

respMsg.setUserNum(albumUserInfoItem.length);

respMsg.setAlbumUserInfoItem(albumUserInfoItem);

}

respMsg.setBackPage(pageBar.getFront());// 上一页

respMsg.setNextPage(pageBar.getBack());// 下一页

respMsg.setSessionId(reqMsg.getSessionId());

respMsg.setSequence(reqMsg.getSequence());

response.write(respMsg);

} catch (ServiceException e) {

ErrorRespMessage errMsg = new ErrorRespMessage();

errMsg.setSequence(reqMsg.getSequence());

errMsg.setSessionId(reqMsg.getSessionId());

response.write(errMsg);

}

return null;

}

}

banq
2007-01-07 11:47

看你excute中代码有两个明显缺点:1 过长 2 if else 太多。

明显是非OO风格。

遇到这种情况, 毫不犹豫去重构,重构出多个方法,可能不是两个方法。