I have the following code which is responsible for the validation in our app (just an example).
Based on the particular tab where user can add person to the database we need to perform additional validation (more fields in each tab).
Fields that always should be validated are name
and surname
so we provided an abstract class with subclasses that are specialized for each tab (i.e. Sourcing). Is it somehow possible to simplify this code by using some design patterns or something ?
abstract public class AbstractPersonValidator {
public boolean isInvalid( Person person ) {
if ( person.getName() == null || person.getName().length() < 3 )
return true;
if ( person.getSurname() == null || person.getSurname().length() < 3 )
return true;
return false;
}
}
Extending:
public class SourcingPersonValidator extends AbstractPersonValidator {
@Override
public boolean isInvalid( Person person ) {
if ( super.isInvalid( person ) )
return true;
if ( person.getTechnology() == null )
return true;
if ( person.getSource() == null )
return true;
return false;
}
}