Validation of business object avoiding/reducing use of if’s and Else’s

Asked

Viewed 915 times

7

I have a problem where I have three types of employee (Funcioa, Funcionariob, Funcionarioc), all will be recorded in a table called Funcio and their respective relationships, however, these types mentioned above, have different validations among themselves. Only with JPA’s Annotations I can’t and I would like to know if there is some Pattern design or something similar to avoid if’s and Else’s to make the validations of the use case, these in turn will be returned to Managedbean to be displayed to the user.

Frameworks of the project: JSF 1.2; EJB 3.0; Eclipselink with JPA 1.0;

Server: jboss 5.1

  • 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

  • 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.

  • 1

    @caioalx The criterion for defining the type of employee is only one field?

  • Exactly. If the same is celetist, permanent staff, surfer and etc.

1 answer

5


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

Code available on Github

  • 1

    Great! The Enum I had already created, but it never crossed my mind to return a validator for the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.