策略模式

Posted zhangcf420

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了策略模式相关的知识,希望对你有一定的参考价值。

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


}
}




















































































































以上是关于策略模式的主要内容,如果未能解决你的问题,请参考以下文章

Redis实现分布式锁(设计模式应用实战)

用于从 cloudkit 检索单列的代码模式/片段

代码片-策略模式+工厂模式

代码片-策略模式+工厂模式

代码片-策略模式+工厂模式

代码片-策略模式+工厂模式