Am I abusing Strategy Pattern in Java?

1

In an application for user authentication via Radius I thought it would be interesting to use Design Patter Strategy with Enum.

So the code looks like this:

public enum TipoAutenticacao {

LIVRE("Acesso Livre"){
    @Override
    public String autentica(LoginService service, String user, String mac) throws NoResultException, AccessException{
        Login login = service.findByUser(user);
        return login.autentica(user, login.getPass());
    }
},
MAC("Filtro de MAC"){
    @Override
    public String autentica(LoginService service, String user, String mac) throws NoResultException, AccessException{
        Login login = service.findByUser(user);
        return login.autentica(user, login.getPass(), mac);
    }
};

private String descricao;


TipoAutenticacao(String descricao){
    this.descricao = descricao;
}

public String getDescricao(){
    return this.descricao;
}

public abstract String autentica(LoginService service , String user, String mac) throws NoResultException, AccessException;
}

Method autentica not Login

    public String autentica(String user, String pass, String mac) throws AccessException {
    if(!this.mac.equals(mac))
        throw new AccessException(TipoLoginResposta.MAC_INVALID);

    return autentica(user, pass);
}

public String autentica(String user, String pass) throws AccessException {
    if(!this.user.equals(user) || !this.pass.equals(pass))
        throw new AccessException(TipoLoginResposta.USER_PASS_INVALID);
    if(bloqueado)
        throw new AccessException(TipoLoginResposta.BLOQUEADO);
    if(!this.getUsuario().isPendenciaFinanceira())
        throw new AccessException(TipoLoginResposta.PENDENCIA_FINANCEIRA);
    return this.pass;
}

TipoLoginResposta

public enum TipoLoginResposta {

USER_PASS_INVALID("Login/Senha Inválidos"),
MAC_INVALID("MAC Inválido"),
PENDENCIA_FINANCEIRA("Pendência Financeira"),
BLOQUEADO("Bloqueado");

private String descricao;

TipoLoginResposta(String descricao){
    this.descricao = descricao;
}

public String getDescricao(){
    return this.descricao;
}
}

The idea of using AccessException passing in the TipoLoginResposta constructor is so that the reason why the user could not connect is later specified in the database.

Using this implementation, am I tying the code somehow?

    
asked by anonymous 15.10.2015 / 00:26

1 answer

1
  

Do I abuse the Strategy Pattern in Java?

The answer is yes. Design patterns should not be inadvertently used. For your case, you could have solved the problem in a simpler way.

The implementation of LIVRE and MAC fault authentication is practically the same, you just added one more parameter.

If a problem can be solved simply, it must be solved in a simple way. In your case, if each type actually had a different implementation and with time new types started to emerge and maintenance started to get difficult, then the design pattern comes in to solve the situation. Although the way you did, every time a new type appears, you will need to change the TipoAtenticacao . The idea of Strategy is precisely not having to do that. The idea is to create a new class and plug it into the system.

    
07.08.2016 / 15:42