Proszę bardzo, zrobiłem sobie walidatory do Nipu, peselu, VIN, kodu pocztowego. Żeby było sprytnie odziałem je w 1 klasie w metodach statycznych:
final class PeselValidator implements Predicate<String> {
@Override
public boolean test(String value) {
return true; //TODO
}
}
.
final class NipValidator implements Predicate<String> {
@Override
public final boolean test(String nip) {
//TODO
return nip.length() > 0 ;
}
}
.
public class ValidatorHolder {
public final static Predicate<String> POSTAL_CODE = postalCode -> {
return postalCode.length() == 6 && postalCode.matches("\\d+{2}-\\d+{3}");
};
public final static Predicate<String> NIP = new NipValidator();
public final static Predicate<String> VIN = new VinValidator();
public final static Predicate<String> PESEL = new PeselValidator();
}
.
użycie:
ValidatorHolder.NIP.test(myValueToValidate);
i proszę bardzo zwalidowaliśmy se nip. A jak chcemy pesel to proszę bardzo:
ValidatorHolder.PESEL.test(myValueToValidate);
czy to jest dobrze zrobione? Czy nie powinno być to zrobione jako Springowe beany?
Kiedy stosować beany zamiast metod statycznych?