Java后端工厂+策略模式使用教程
然后使用工厂类的getHandler方法,将题目Type作为参数传入进去,即可返回对应的策略类handler(处理器),例如:我传入的type是1,那么它返回的就是单选题的策略类,然后再通过add方法,即可添加数据。一个工厂对应了4种策略(单选题,填空题,简答题,多选题),根据传入的type进行自动映射处理,单选的调用单选的service,多选的调用多选的service,我这里只写了单选策略和多选
·
例如:对题目进行工厂+策略模式的处理
一个工厂对应了4种策略(单选题,填空题,简答题,多选题),根据传入的type进行自动映射处理,单选的调用单选的service,多选的调用多选的service,我这里只写了单选策略和多选策略。
第一步:先定义一个枚举,用来识别题目类型1单选,2多选
package com.qiao.enums;
/**
* 题目类型枚举
*/
public enum SubjectTypeEnum {
MULTIPLE_CHOICE("多选题", 2),
SINGLE_CHOICE("单选题", 1);
private String name;
private int index;
private SubjectTypeEnum(String name, int index) {
this.name = name;
this.index = index;
}
public static SubjectTypeEnum getName(int index) {
for (SubjectTypeEnum c : SubjectTypeEnum.values()) {
if (c.index == index) {
return c;
}
}
return null;
}
}
第二部:创建一个handler包,在handler包下创建一个subject包,在subject包下创建一个公共的接口,接口中插入题目传入的参数应该是不同题目共有的字段实体类。
package com.qiao.handler.subject;
import com.qiao.dto.SubjectInfo;
import com.qiao.enums.SubjectTypeEnum;
public interface SubjectTypeHandler {
/**
* 枚举身份的识别,1为单选,2为多选
* @return
*/
SubjectTypeEnum getHandlerType();
/**
* 实际的题目插入
* @param subjectInfo
*/
void add(SubjectInfo subjectInfo);
}
第三步:创建策略类(单选题,多选题),继承第二步创建的类,并重写类中的方法,用返回的SubjectTypeEnum来告知是哪个类型的题目。(用的是上面创建的枚举)
package com.qiao.handler.subject;
import com.qiao.converter.SubjectInfoConverter;
import com.qiao.dto.SubjectInfo;
import com.qiao.entity.SingleChoice;
import com.qiao.enums.SubjectTypeEnum;
import com.qiao.service.impl.SingleChoiceServiceImpl;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 单选题目的策略类
*/
@Component
public class SingleTypeHandler implements SubjectTypeHandler{
@Resource
private SingleChoiceServiceImpl singleChoiceServiceImpl;
@Override
public SubjectTypeEnum getHandlerType() {
// 告知对放,返回单选题目的枚举类型
return SubjectTypeEnum.SINGLE_CHOICE;
}
@Override
public void add(SubjectInfo subjectInfo) {
SingleChoice singleChoice = SubjectInfoConverter.INSTANCE.
converBoToCategory(subjectInfo);
// 实际往表里插入的数据
singleChoiceServiceImpl.insert(singleChoice);
}
}
package com.qiao.handler.subject;
import com.qiao.dto.SubjectInfo;
import com.qiao.enums.SubjectTypeEnum;
/**
* 多选题目的策略类
*/
public class MultipleTypeHandler implements SubjectTypeHandler{
@Override
public SubjectTypeEnum getHandlerType() {
return SubjectTypeEnum.MULTIPLE_CHOICE;
}
@Override
public void add(SubjectInfo subjectInfo) {
}
}
第四步:创建工厂类
package com.qiao.handler.subject;
import com.qiao.enums.SubjectTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 题目类型工厂
*/
@Component//这里要打注解,交给spring容器管理
public class SubjectTypeHandlerFactory implements InitializingBean {
//这里用的Spring的特性,自动注入,会把所有实现了SubjectTypeHandler接口的类都注入到这个集合中
@Resource
private List<SubjectTypeHandler> subjectTypeHandlersList;
//创建一个Map,用来存储题目类型和对应的处理器
private Map<SubjectTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();
//根据题目类型获取对应的处理器
public SubjectTypeHandler getHandler(int subjectType) {
SubjectTypeEnum subjectTypeEnum = SubjectTypeEnum.getName(subjectType);
return handlerMap.get(subjectTypeEnum);
}
/**
* 初始化方法,在bean初始化后调用
* 这里主要是在bean初始化后把handlerMap初始化,把所有实现了SubjectTypeHandler接口的类都注入到handlerMap中
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlersList) {
handlerMap.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);
}
}
}
第五步:当我们使用的时候,只需要将第四步创建的工厂类注入进去,然后使用工厂类的getHandler方法,将题目Type作为参数传入进去,即可返回对应的策略类handler(处理器),例如:我传入的type是1,那么它返回的就是单选题的策略类,然后再通过add方法,即可添加数据。
更多推荐
所有评论(0)