我想完成一项任务,我的工作流程是输入(参数+配置) - >验证(验证后,返回操作) - >操作处理 - >输出。现在我正在输入和验证部分工作。参数是命令行参数。加载配置文件后,我可以获取配置详细信息,该命令行将提供此文件路径。现在我要设计一个Validation Factory类,它将根据命令行参数中指定的操作类型返回验证器实例,可能是内容验证器或状态更改验证器等。现在,我坚持一步,即我如何在输入步骤中适合配置加载过程,以便在验证步骤中验证少数所需的配置。验证工厂类设计
我分享我的代码 -
这是我的验证工厂类
public class ValidatorFactory {
private ValidatorFactory() {
}
public static Validator getInstance(CommandLine cmd) throws BadArgumentException {
String operationType = cmd.getOptionValue("op");
......
switch (OperationType.valueOf(operationType.toUpperCase())) {
case PUBLISH: validator = new StateChangeValidator(cmd);
//here i want to pass configuration to the constructor, but i want to know how can i use my configuration loader which will load and returns me configuration.
}
return validator;
}
}
问题1)可以确认工厂的意思完整的上下文加载配置文件?我的意思是说,我可以在getinstance()部分中使用我的加载器吗?它可以意味着完整吗?
这是我的验证类: -
public abstract class AbstractValidator
implements Validator {
protected CommandLine cmd;
protected Properties configuration;
public AbstractValidator(CommandLine cmd, Properties configuration) {
this.configuration = configuration;
this.cmd = cmd;
}
......
}
public class StateChangeValidator extends AbstractValidator {
public StateChangeValidator(CommandLine cmd, Properties properties) {
super(cmd, properties);
}
......
}
,这是我的装载机类
public final class ConfigurationFileLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationFileLoader.class);
public static final Properties getConfiguration(String filePath) throws BadFileException {
//validate configuration file, load and return configuration
}
}
我如何设计流量?因为配置文件的加载取决于命令行提供的一个输入。
你有很多代码,从* hard *开始阅读/理解解释。我建议你简化整个事情(例如,参见[mcve])。 – GhostCat
您还没有提供验证器界面,它几乎是您问题的核心。如果它包含validate(Properties p)方法,则可以调用创建的验证器来验证加载的属性。 – daniu
@GhostCat请检查。 –