Am I abusing Strategy Pattern too much in Java?

Asked

Viewed 165 times

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 went 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 in the 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 the AccessException passing the builder TipoLoginResposta is to be specified later in the database why the user could not connect.

Using this implementation I’m tying the code somehow?

  • 1

    In the case of TipoAutenticacao if it can avoid being a enum better, in my opinion two classes that implement an interface with the method are enough autentica().

  • But if you need to add other ways to authenticate? I would have to have a class for each type of authentication.. This would generate several classes, no?

  • 4

    It would generate, just as in the current way you would need several instances of Enum Typo. But without violating the open/closed principle, because it would not be changing an Enum but adding classes.

  • 3

    Not directly related to the question, but why do you copy much of the code between the autentica with 2 or 3 parameters? Wouldn’t it be better to do something like { if(!this.mac.equals(mac)) throw ...; return autentica(user, pass); } The order of the tests would be slightly altered, however, maybe not what you need, but stay there as a suggestion.

  • @Piovezan Thanks! I will consider your tip. This open/closed principle is the right SOLID concept? I’m going to study it.

  • @mgibsonbr Really! I applied this to other parts of the code, but I missed that part. Thank you!

Show 1 more comment

1 answer

1


I am abusing the Strategy Pattern in Java?

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

The implementation of the authenticated LIVRE and of MAC is pretty much the same, you just added one more parameter.

If a problem can be solved in a simple way, it must be solved in a simple way. In your case, if each type really had a different implementation and over time new types started to appear and maintenance started to get difficult, then yes enter the design pattern to resolve the situation. Although the way you did, every time a new guy comes along, you’re gonna need to change the TipoAtenticacao. The idea of Strategy is just not to have to do it. The idea is to create a new class and plug it into the system.

Browser other questions tagged

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