码迷,mamicode.com
首页 > 其他好文 > 详细

策略模式

时间:2019-01-24 18:47:07      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:service   ack   try   res   oid   commons   auto   bean   cat   

业务场景:操作类型三种。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) {
//方法体


}
}

策略模式

标签:service   ack   try   res   oid   commons   auto   bean   cat   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!