3
我有一个问题,我试图解决。我需要能够根据用户的输入返回实现。我已经研究过使用抽象工厂模式,但我不确定它是否是最好的方法(或者如果Spring可以帮助我一点)。如何在运行时选择bean实现在春天
这里的工厂将返回接口:
public interface Flow {
List<Message> execute(String sessionKey);
}
和1实现该接口的:
@Component("AssignSeatFlow")
public class AssignSeatFlow implements ChatbotFlow {
private SeatService seatService;
@Autowired
public AssignSeatFlow(final SeatService seatService) {
this.seatService = seatService;
}
@Override
public List<Message> execute(String sessionKey) {
// Implementation here
}
}
我现在的工厂接口:
public interface FlowFactory {
Flow getFlow(final String intentCode);
}
及其实现:
@Component
public class FlowFactoryImpl implements FlowFactory {
@Resource("AssignSeatFlow")
private Flow assignSeatFlow;
@Override
public Flow getFlow(final String intentCode) {
if(StringUtils.isNullOrEmpty(intentCode)) {
throw new IllegalArgumentException("Intent Code cannot be empty");
}
switch (intentCode.toUpperCase()) {
case "AssignSeatFlow":
return assignSeatFlow;
default:
throw new IllegalArgumentException("Unable to determine flow");
}
}
}
这个看起来并不理想的原因是,当我添加更多的流程时,工厂将变得更大,每次我都会修改它。我也不是Autowiring字段的粉丝,因为它使测试更加复杂和不太明确。
感谢您的任何反馈意见。
我听说这不是一个好主意,但它确实使它看起来非常干净(并且可维护!) – StylePotato