Business object validation avoiding / reducing use of if's and else's

7

I have a problem where I have three types of employee (EmployeeA, EmployeeB, EmployeeC), all of which will be stored in a table named Employee and their respective relationships, but these types mentioned above have different validations between them. Only with annotations in JPA I can not and I would like to know if there is any design pattern or something similar to avoid if's and else's to validate the use case, these will in turn be returned to the ManagedBean to be displayed to the user. / p>

Project frameworks: JSF 1.2; EJB 3.0; EclipseLink with JPA 1.0;

Server: jboss 5.1

    
asked by anonymous 23.06.2014 / 17:02

1 answer

5

You could use a Enum to do the validation or return a validator to enforce 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;
}

Validator implementations

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

    }

}

Result:

  

John - > br.com.starcode.validationenum.CeletistaValidator

     

Joseph - > br.com.starcode.validationinfo.PermanentValidator

     

Maria - > br.com.starcode.validacaoenum.SurfistaValidator

Code available on GitHub

    
23.06.2014 / 19:38