起初业务简单、随着业务增长所有相关业务代码处理逻辑 都放置同一个对象中,通过策略模式以不通分类实现具体业务
public interface IPlanVideoStrategy {
String execute(Integer typeCode, Integer nodeId, List<String> qzTimeList);
}
public class PlanPlayBackContext {
private IPlanVideoStrategy strategy;
private SysNodeMapper nodeMapper;
// 设置策略工厂并通过set构造注入bean
public void setStrategy(IPlanVideoStrategy strategy) {
this.strategy = strategy;
if(strategy instanceof StartPlanVideoStrategy){
((StartPlanVideoStrategy) strategy).setNodeMapper(nodeMapper);
} else if{
...
}
}
// 构造器
public PlanPlayBackContext(SysNodeMapper nodeMapper) {
this.nodeMapper = nodeMapper;
}
public String executeStrategy(Integer typeCode, Integer nodeId, List<String> qzTimeList) {
return strategy.execute(typeCode, nodeId, qzTimeList);
}
}
@Component
public class StartPlanVideoStrategy implements IPlanVideoStrategy {
private SysNodeMapper nodeMapper;
@Override
public String execute(Integer typeCode, Integer nodeId, List<String> qzTimeList) {
sout("hello");
}
public void setNodeMapper(SysNodeMapper nodeMapper) {
this.nodeMapper = nodeMapper;
}
}
// main
private String createPlanPlayBack(Integer nodeId, List<String> qzTimeList){
SysNode sysNode = nodeMapper.selectById(nodeId);
Integer typeCode = this.getTypeCode(sysNode.getStreamType(), 3);
PlanPlayBackContext context = new PlanPlayBackContext(nodeMapper);
context.setStrategy(new StartPlanVideoStrategy());
return context.executeStrategy(typeCode, nodeId, qzTimeList);
}