首页 > 其他 > 详细

策略模式

时间:2019-01-24 18:34:55      阅读:201      评论:0      收藏:0      [点我收藏+]

业务场景:操作类型三种。0、1、2、为了层次清晰,减少if判断,所以使用。

目录结构:

 

技术分享图片

 

1、server类

@Transactional
public void saveQuestion(QuestionRequest request) {
IQuestionSaveHandler handler = null;

try {
handler = SpringContextHolder.getBean(IQuestionSaveHandler.QUESTION_SAVE_HANDLER_BEAN_PREFIX+request.getHandlerType(), IQuestionSaveHandler.class);
}catch(Exception e) {
throw new IntfException("未知的处理类型 "+request.getHandlerType());
}
handler.questionSave(request);

}

2、IQuestionSaveHandler类

public interface IQuestionSaveHandler {

public final static String QUESTION_SAVE_HANDLER_BEAN_PREFIX = "questionSaveHandler_";
/**
* 问题保存
*/
public void questionSave(QuestionRequest request);
}

3、AbstractQuestionSaveHandler类

package com.yemast.road.roadpatrol.server.service.strategy.questionsave;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import com.yemast.road.roadpatrol.server.service.strategy.IQuestionSaveHandler;
import com.yemast.road.roadsso.common.exception.IntfException;
import com.yemast.road.roadsso.common.util.CommonUtil;

/**
* 抽象类
*
*
*/
public abstract class AbstractQuestionSaveHandler implements IQuestionSaveHandler {
@Autowired
protected SectionService sectionService;
@Autowired
protected InformationService informationService;

@Autowired
protected UserQuestionService userQuestionService;

@Override
public void questionSave(QuestionRequest request) {

// 方法体


public abstract void doProcess(Question question, QuestionRequest request);
}
4、QuestionSaveHandlerImmediate 类

package com.yemast.road.roadpatrol.server.service.strategy.questionsave;

import com.yemast.road.roadpatrol.core.entity.patrol.Question;
import com.yemast.road.roadpatrol.core.entity.patrol.QuestionProcess;


@Service(IQuestionSaveHandler.QUESTION_SAVE_HANDLER_BEAN_PREFIX+"0")
public class QuestionSaveHandlerImmediate extends AbstractQuestionSaveHandler{

@Override
public void doProcess(Question question, QuestionRequest request) {
//方法体

}

}
5、QuestionSaveHandlerInstructions 类
package com.yemast.road.roadpatrol.server.service.strategy.questionsave;

import com.yemast.road.roadpatrol.core.response.PushParams;
import org.springframework.stereotype.Service;

import com.yemast.road.roadpatrol.core.entity.patrol.Question;
import com.yemast.road.roadpatrol.core.entity.patrol.QuestionProcess;



@Service(IQuestionSaveHandler.QUESTION_SAVE_HANDLER_BEAN_PREFIX+"2")
public class QuestionSaveHandlerInstructions extends AbstractQuestionSaveHandler{

@Override
public void doProcess(Question question, QuestionRequest request) {

//方法体
}
}
6、QuestionSaveHandlerNotify 类

package com.yemast.road.roadpatrol.server.service.strategy.questionsave;

import com.yemast.road.roadpatrol.core.response.PushParams;
import org.springframework.stereotype.Service;

import com.yemast.road.roadpatrol.core.entity.patrol.Question;
import com.yemast.road.roadpatrol.core.entity.patrol.QuestionProcess;
import com.yemast.road.roadpatrol.core.entity.patrol.Task;


@Service(IQuestionSaveHandler.QUESTION_SAVE_HANDLER_BEAN_PREFIX+"1")
public class QuestionSaveHandlerNotify extends AbstractQuestionSaveHandler{



@Override
public void doProcess(Question question, QuestionRequest request) {
//方法体


}
}

策略模式

原文:https://www.cnblogs.com/zhangcf420/p/10315975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!