简体   繁体   中英

Validation Factory Class Design

I want to accomplish one task, my work flow is input (args + configuration) -> validation (after validation,return operation) -> processing on operation -> output. Now i am working in input and validation section. args is command line arguments. I can grab the configuration details after loading configuration file and this file path will be supplied from command line. Now i am going to design one Validation Factory class which will return me an instance of validator based on the operation type specified from command line arguments, may be it will be content validator or state change validator etc. Now i am stuck with one step, ie how can i fit configuration loading process in the input step so that i can validate few required configuration in the validation steps.

I am sharing my code-

This is my validation factory class

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;
    }

}

Question 1) Can validation factory is meaning full context to load configuration file? I mean to say, can i use my loader in the getinstance() section? can it be meaning full?

These are my Validation classes:-

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);
    }
   ......
}

and this is my loader class

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

    }

}

How can i design the flow? because loading of configuration file is depended on one input supplied from command line.

Okay, Based on my understanding of what you are doing. I would make below changes:

i) Do not mix configuration and validation. Break it down to two parts - Configuration loader and validation.

ii) Do not load the file every time. You can maintain a cache with a good eviction policy. I don't think your config file location will change every time.

iii) Do not create a new validator everytime in ValidatorFactory . You can have a single instance of each validator. To accomplish this you will have to make it stateless. Have a no-arg constructor (that is all) and have setters to set configurations and data to validate.

iv) So, in first step you load all configurations and then get a validator. Set cmd and configurations and then call validate method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM