Doubt: It is advisable to use a class not as an Entity but as Functionality?

0

Very good.

I have a method that checks if all TextField is complete and that it returns a boolean based on that. My idea is that I want to use this method in several classes to do it with different windows (frames), since each one has different fields.

The question is, I wanted to use the previous method in a Class as Auxiliares and specify it as estatic , and I would just pass it a list of fields. But the problem is that it does not leave me, because of the non-static context.

Trying to solve this (I work), I decided to create a normal class and create an instance of it to use this method in question. But this makes me noise, because I do not know if it would be nice to create a Class that does not represent an entity a Functionality.

I hope you can clarify what I'm trying to do. Any questions I will answer as soon as possible. Thank you!

MetodosAuxiliares ma = new MetodosAuxiliares(campos);

@FXML
    private void btnGuardar_Click(MouseEvent event) throws IOException {
        if (ma.informacionDeCamposCompleta(campos)) {
            Ventana.cerrarVentanaActual(btnGuardar, Formularios.FRM_VENDEDORES_EDICION);
            Ventana.abrirVentana(Formularios.FRM_VENDEDORES, getClass());
        }

    }

I'll leave the class you use in case you want to check something

public class MetodosAuxiliares {

    private ArrayList<Control> camposTxt;

    public MetodosAuxiliares() {
        camposTxt = new ArrayList<>();
    }

    public boolean informacionDeCamposCompleta(ArrayList<Control> campos) {
        return validarCampos(campos);
    }

    private boolean validarCampos(ArrayList<Control> camposTxt) {
        for (Control campo : camposTxt) {
            if (campo instanceof TextField) {
                TextField c = (TextField) campo;
                if (c.getText().isEmpty()) {
                    //Tira una alerta informando que campo falta completar
                    validarFalla(campo);
                    return false;
                }
            }
            if (campo instanceof TextArea) {
                TextArea c = (TextArea) campo;
                if (c.getText().isEmpty()) {
                    validarFalla(campo);
                    return false;
                }
            }
        }
        return confirmarGuardado();
    }

    private void validarFalla(Control campo) {
        String tooltip = campo.getTooltip().getText();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Adm Concesionaria");
        alert.setHeaderText("No completo todos los campos requeridos");
        alert.setContentText("El campo " + tooltip + " no puede estar vacio");
        alert.showAndWait();
        campo.requestFocus();
    }

    private boolean confirmarGuardado() {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Adm Concesionaria");
        alert.setHeaderText("Esta apunto de guardar los datos, desea contiuar?");
        Optional<ButtonType> btnPresionado = alert.showAndWait();
        return btnPresionado.get() == ButtonType.OK;
    }
    
asked by GonzaloM 01.12.2018 в 17:34
source

1 answer

1

If you just want to use the functions of a class, it is best that you convert the methods of the class to static so that you can use them without having to instantiate the class whenever you need them.

I give you an example, this class has two methods, the first one is used to verify if a list is not empty and the second one delegates a task as in your case.

public class ClaseDeFunciones {

    public static boolean arrayNoEstaVacio(List<String> palabras){
        return comprobacionDelegada(palabras);
    }

    private static boolean comprobacionDelegada(List<String> palabras) {
        return palabras.size() >0;
    }
}

Subsequently, the implementation class (can be a Main or whatever you want), will call directly to the methods you want, without the need to instantiate the class.

public class Main {

    public static void main(String args[]){
        ArrayList<String> animales = new ArrayList<>();
        animales.add("Gato");
        if (ClaseDeFunciones.arrayNoEstaVacio(animales)){
            System.out.println("La lista no está vacía.");
        }
    }
}

To conclude, you can have attributes and methods, instance and static in the same class, but these will be called in different ways.

Static method:

if(TuClase.comportamientoOComprobador(algunArgumento)){
    //Lo que deseas hacer
};

Instance method:

TuClase referencia = new TuClase();
if(referencia.comportamientoOComprobador(algunArgumento)){
    //Lo que deseas hacer
};

I hope you find it useful.

    
answered by 01.12.2018 / 18:05
source