业务:近期活动开启,需要用户去做n个任务,做完任务则可以获取对应奖品,也需要支持查询用户任务完成情况

1、定义任务接口,2个任务中共同需要的部分,分别是任务处理,获取任务详情

public interface TaskInterface {
	// 任务详情(获取任务是否完成)
    TaskInfo task(long uId);
	// 处理任务
    void handle(long uid);
	// 任务标识
    String getType();
}

定义枚举类

public enum TaskEnum {
    TASK_ONE(1, "TaskOne"),
    TASK_TWO(2, "TaskTwo")

    private final int id;
    private final String type;

    TaskEnum(int id, String type) {
        this.id = id;
        this.type = type;
    }

    public int getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    // 根据 id 获取类型
    private static final Map<Integer, TaskEnum> ID_MAP = new HashMap<>();
    static {
        for (TaskEnum e : values()) {
            ID_MAP.put(e.getId(), e);
        }
    }

    public static TaskEnum fromId(int id) {
        return ID_MAP.get(id);
    }
    
    public static final Map<Integer, String> TASK_TYPE_MAP;
    static {
        Map<Integer, String> map = new HashMap<>();
        for (TaskType task : values()) {
            map.put(task.getId(), task.getType());
        }
        TASK_TYPE_MAP = map;
    }
}

2、具体任务实现任务接口

2.1、实现1号任务

@Component
public class TaskOne implements TaskInterface {
    @Override
    public TaskInfo task(long userId) {
        System.out.println("输出1号任务的完成情况");
    }
    
    @Override
    public void handle(long uid, long time, long tid, int count) {
        System.out.println("处理1号任务是否可以完成任务");
    }
    
    @Override
    public String getType() {
        // 返回当前任务的标识
        return TaskEnum.TASK_ONE.getType();
    }
}

2.2、实现2号任务

@Component
public class TaskTwo implements TaskInterface {
    @Override
    public TaskInfo task(long userId) {
        System.out.println("输出2号任务的完成情况");
    }
    
    @Override
    public void handle(long uid, long time, long tid, int count) {
        System.out.println("处理2号任务是否可以完成任务");
    }
    
    @Override
    public String getType() {
        // 返回当前任务的标识
        return TaskEnum.TASK_TWO.getType();
    }
}

3、定义任务工厂

@Component
public class TaskFactory {
    private final Map<String, TaskInterface> calculateServiceStrategyMap = new ConcurrentHashMap<>();

    // springboot会将实现了TaskInterface的类都注入到这个map
    @Autowired
    private Map<String, TaskInterface> taskBeans;

    @PostConstruct
    public void setStrategyContextMap() {
        taskBeans.values().forEach(task -> calculateServiceStrategyMap.put(task.getType(), task));
    }

    public TaskInterface getTaskInterface(String type) {
        return calculateServiceStrategyMap.get(type);
    }
}

4、使用 查询任务

    public List<TaskInfo> getTask(long uid) {
        List<TaskInfo> result = new ArrayList<>;
        for (int i = 1; i <= TASK_TYPE_MAP.size(); i++) {
            TaskInterface taskInterface = TaskFactory.getTaskInterface(TASK_TYPE_MAP.get(i));
            TaskInfo task = taskInterface.task(uid);
            result.add(task);
        }
        return result;
    }

5、使用 处理任务

    public void TaskHand(int taskId, long uid) {
        try {
            TaskInterface taskInterface = TaskFactory.getTaskInterface(TASK_TYPE_MAP.get(taskId));
            taskInterface.handle(uid);
        } catch (Exception e) {
            log.error("TaskHand error", e);
        }
    }

6、具体任务入口调用TaskHand入口既可,比如,签到任务,则当用户签到完成后,调用TaskHand方法

7、后续如果有新增任务,则直接一直新增2步骤既可

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐