Possibility of application of design standard

Asked

Viewed 83 times

3

After a login effective in the application I am building I return a boolean[] with all the accesses that the user has.

// Armazena o controle de acesso do usuário
LoginDAO logindao = new LoginDAO(conexao);
boolean[] acessos = logindao.controleAcesso(codigoUsuario);

Now when applying these accesses, instantiating only what can be used, I have a code full of ifs (totalling 17) that I would like to remove or simplify, see:

/**
 * Aplica o controle de acesso às telas do sistema. Criando apenas
 * as necessárias.
 * @param acessos Lista com os acessos
 */
private void aplicarAcessos(boolean[] acessos) {
    if(acessos[0]) {
        tabAcervo = new TabPane();

        ObservableList<Tab> abasAcervo = tabAcervo.getTabs();

        if(acessos[1]) {            
            abasAcervo.add(new TelaMovimentacao().constroi());
        }

        if(acessos[2]) {
            abasAcervo.add(new TelaConsulta().constroi());
        }

        if(acessos[3]) {
            abasAcervo.add(new TelaReserva().constroi());
        }

    }
    // [...]
}

Note: All Screens obey the interface Canvas:

public interface Tela {

    Tab constroi();
}

There is a design pattern that allows me to know which screen to instantiate without needing that amount of ifs? Or any technique that allows me to simplify this code.

1 answer

4


Look, you can’t tell without knowing the problem deeply, probably more than you know right now. What may look like it fits, then there’s a requirement that I didn’t know and then what I gave is no more.

You’re probably looking for Abstract Factory. Make sure he gets to you. I think it makes the application so complex that even the way you’re doing it is simpler, but it has its use when the natural complexity is great.

One of the ways can be as below, which has limitations (note that I created the screens and use on demand as a pool, has different shapes that may be better than this, for example creation can be on demand saving memory and allowing there to exist more than one instance of the same class without conflicts).

import java.util.*;

class Program {
    private static ArrayList<Tela> telas = new ArrayList<Tela>();
    public static void main (String[] args) {
        telas.add(new TelaConsulta());
        telas.add(new TelaReserva());
        for (String item : aplicarAcessos(new boolean[] { true, true })) System.out.println(item);
    }
    private static ArrayList<String> aplicarAcessos(boolean[] acessos) {
        ArrayList<String> abasAcervo = new ArrayList<>();
        for (int i = 0; i < acessos.length; i++) if (acessos[i]) abasAcervo.add(telas.get(i).constroi());
        return abasAcervo;
    }
}

interface Tela {
    String constroi();
}

class TelaConsulta implements Tela {
    public String constroi() {
        return "Tela Consulta";
    }
}
class TelaReserva implements Tela {
    public String constroi() {
        return "Tela Reserva";
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Take a look at Class.

If you want screens to be added without changing the logic of the code it is possible to make the classes that inherit this interface have a logging method in the array which would be "global", search for Service Locator.

You can use reflection and see all the classes that implement this interface and create instance for each of them. You’d have to get all the application packages first. It slows down. You can have a database or file with the names that need to be instantiated, but I don’t like the idea, although it gets a little faster, nothing good yet.

Honestly it is less work, it is more performative and simpler to do in hand, even if it is not the most elegant.

It is possible to create a code generator that analyzes the classes that implement the interface and creates the array or ifs or cases with each class.

If I can think of any other way I’ll edit it here. As I said it has numerous forms, and only a few may be interesting to your case.

  • The intention is to really save memory creating on demand, I have 4 panels each with 3-4 Screens. The most "heavy" part is in the building(), perhaps instantiating everything and using only the necessary?

Browser other questions tagged

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