You could use a Enum
validate or return a validator to reinforce that each type of employee properly implements the validation.
I have prepared a simple example, below...
interface FuncionarioValidator
public interface FuncionarioValidator {
void validate(Funcionario f) throws Exception;
}
Implementations of the validators
public class CeletistaValidator implements FuncionarioValidator {
@Override
public void validate(Funcionario f) throws Exception {
System.out.println(f.getNome() + " -> " + getClass().getName());
}
}
public class QuadroPermanenteValidator implements FuncionarioValidator {
@Override
public void validate(Funcionario f) throws Exception {
System.out.println(f.getNome() + " -> " + getClass().getName());
}
}
public class SurfistaValidator implements FuncionarioValidator {
@Override
public void validate(Funcionario f) throws Exception {
System.out.println(f.getNome() + " -> " + getClass().getName());
}
}
Enum TipoFuncionario
public enum TipoFuncionario {
Celetista(new CeletistaValidator()),
QuadroPermanente(new QuadroPermanenteValidator()),
Surfista(new SurfistaValidator());
private FuncionarioValidator validator;
private TipoFuncionario(FuncionarioValidator validator) {
this.validator = validator;
}
public FuncionarioValidator getValidator() {
return validator;
}
}
Example of Funcionario
public class Funcionario {
private Integer id;
private String nome;
private TipoFuncionario tipo;
public Funcionario(Integer id, String nome, TipoFuncionario tipo) {
this.id = id;
this.nome = nome;
this.tipo = tipo;
}
public Integer getId() {
return id;
}
public String getNome() {
return nome;
}
public TipoFuncionario getTipo() {
return tipo;
}
}
Performing the validation
I implemented a very simple test:
public class FuncionarioTest {
@Test
public void test() throws Exception {
//cria lista de funcionários
List<Funcionario> funcionarios = new ArrayList<Funcionario>();
funcionarios.add(new Funcionario(1, "João", TipoFuncionario.Celetista));
funcionarios.add(new Funcionario(2, "José", TipoFuncionario.QuadroPermanente));
funcionarios.add(new Funcionario(3, "Maria", TipoFuncionario.Surfista));
//valida todos
for (Funcionario funcionario : funcionarios) {
funcionario.getTipo().getValidator().validate(funcionario);
}
}
}
Upshot:
João -> br.com.starcode.validacaoenum.Celetistavalidator
José -> br.com.starcode.validacaoenum.Quadropermanentevalidator
Maria -> br.com.starcode.validacaoenum.Surfistavalidator
This question may be helpful: http://answall.com/questions/4731/por-que-em-algumas-situates%C3%A7%C3%B5es-ifs-s%C3%A3o-considered-bad
– Luiz Vieira
To build the employee according to their type, I created a Builder with Strategy, because I can create an employee I want, using polymorphism and hook methods. I think I’ll have to add a specific validation method for each type of employee in their respective classes and before building them, and insert them into the interface that they all implement. So, in addition to building the employee according to the type calling the method once, I do the same with validation.
– caioalx
@caioalx The criterion for defining the type of employee is only one field?
– utluiz
Exactly. If the same is celetist, permanent staff, surfer and etc.
– caioalx