dimanche 5 février 2023

initialize static variable with non-static member?

I'm studying design patterns on Java, and by trying to implement I found something that I would like to know more about.

Is it a good idea to initialize static variables in a constructor?

The idea is to have a factory that creates instances of validators (factory pattern), that can be accessed in a static way later in the code to avoid passing a validatorFactory as parameter.
I want to avoid creating new instances of validators, since, after knowing the validatorConfig, they can't be different, so I need only one instance of them. (Can this be called Singleton Pattern?).

Factory:

public class ValidatorFactory {
    
    public ValidatorFactory(final ValidatorConfig validatorConfig) {
        PARENT_VALIDATOR = new ParentValidator(validatorConfig);
        FIELD_VALIDATOR = new FieldValidator(validatorConfig);
        OBJECT_VALIDATOR = new ObjectValidator(validatorConfig.getObjectValidatorConfig());
        STRING_VALIDATOR = new StringValidator(validatorConfig.getStringValidatorConfig());
        COLLECTION_VALIDATOR = new CollectionValidator(validatorConfig.getCollectionValidatorConfig());
        MAP_VALIDATOR = new MapValidator(validatorConfig.getMapValidatorConfig());
        SKIP_VALIDATOR = new SkipValidator(validatorConfig.getSkipValidatorConfig());
        VALIDATE_THEN_SKIP_VALIDATOR = new ValidateThenSkipValidator(validatorConfig.getValidateThenSkipValidatorConfig());
    }
    
    public static ParentValidator PARENT_VALIDATOR;
    public static FieldValidator FIELD_VALIDATOR;
    public static ObjectValidator OBJECT_VALIDATOR;
    public static StringValidator STRING_VALIDATOR;
    public static CollectionValidator COLLECTION_VALIDATOR;
    public static MapValidator MAP_VALIDATOR;
    public static SkipValidator SKIP_VALIDATOR;
    public static ValidateThenSkipValidator VALIDATE_THEN_SKIP_VALIDATOR;
    
}

I have a method that takes as a parameter a configurator class:

public static void validateObjectBFS(@NonNull Object root, @NonNull ValidatorConfig validatorConfig) throws ValidatorException {
    ValidatorBFS validatorBFS = new ValidatorBFS(validatorConfig);
    validatorBFS.isValid(root);
}

ValidatorBFS:

public class ValidatorBFS {

    private final ValidatorConfig validatorConfig;
    private final ValidatorFactory validatorFactory; // <-- 

    public ValidatorBFS(final ValidatorConfig validatorConfig) {
        this.validatorConfig = validatorConfig;
        this.validatorFactory = new ValidatorFactory(validatorConfig); // <-- 
    }

    public void isValid(final Object root) throws ValidatorException {
    
        RootInfo rootInfo = new RootInfo(root);
        Class<?> rootClass = root.getClass();

        ParentInfo parentInfo = new ParentInfo(rootInfo, rootInfo.toString(), rootClass, ROOT, root, getFields(rootClass)));

        ValidatorFactory.PARENT_VALIDATOR.execute(parentInfo); // <-- 

    }
    
}

ParentValidator:

public class ParentValidator {

    private final ObjectConfigCase condition;

    public ParentValidator(final ValidatorConfig validatorConfig) {
        condition = getCaseCondition(validatorConfig.getObjectValidatorConfig());
    }

    public List<ParentInfo> execute(final ParentInfo parentInfo) throws ValidatorException {
        if(ValidatorFactory.SKIP_VALIDATOR.execute(parentInfo)){ // <-- 
            return Collections.emptyList();
        }
        return ValidatorFactory.FIELD_VALIDATOR.execute(condition); // <-- 
    }
}

In this tutorial can-we-initialize-static-variables-in-a-constructor-in-java seems to work, but I would like to know if this is a good way to handle this situation, and if there are some best alternatives.

Aucun commentaire:

Enregistrer un commentaire