Pass parameters to the Constructor or a method

3

My doubt is as follows, should I pass the parameters obtained from a JTextField to the setter methods of the User class? or pass them directly to the constructor of the User class, what would be the difference?

//Esta es la clase registro pero resumida//

 public class Registro extends JFrame implements ActionListener{

private JTextField campoNombre,campoApellido,campoCorreo,campoNombreUsuario;
private JPasswordField campoContrasena;
private JRadioButton masculino,femenino;
private ButtonGroup grupo;
private JButton finalizar,limpiar;


public Registro(){

    super("Registro OO");
    Toolkit posicionMonitor = Toolkit.getDefaultToolkit();
    Dimension tamanio = posicionMonitor.getScreenSize();
    int anchoVentana = tamanio.width;
    int altoVentana = tamanio.height;
    setBounds(((anchoVentana/2)-(400/2)),((altoVentana/2)-(500/2)),400,500);
    this.setResizable(false);

    iniciarComponentes();
}

 public void actionPerformed(ActionEvent e) {

    if(e.getSource().equals(limpiar)){
        limpiaCampos();
    }

    else if(e.getSource().equals(finalizar)){
        tomaDatos();
    }
}
public void tomaDatos(){
    String sex ="";
    if(masculino.isSelected())
        sex = "Masculino";
    else
        sex = "Femenino";

    Usuario nuevoUsuario = new Usuario(campoNombre.getText(),campoApellido.getText(),campoNombreUsuario.getText(),
            campoCorreo.getText(),sex,campoContrasena.getPassword());

    JOptionPane.showMessageDialog(null, "Registro Exitoso","Registrado",JOptionPane.DEFAULT_OPTION);

    limpiaCampos();
}
    
asked by Debbewas 16.10.2016 в 04:29
source

2 answers

2

I personally do not like having builders with many parameters because:

  • Complicates reading and understanding the code.
  • Add complexity to inheritance that is usually not useful.
  • Therefore, I think it would be more convenient to initialize each user with some "mandatory" parameters (for example your username and password) to ensure the consistency of each object and leave the rest of the data as something optional manageable with setters. The code would be much more readable. It would be something like this:

    String nombreUsuario = campoNombreUsuario.getText();  
    String pass = campoContrasena.getPassword();
    
    Usuario nuevoUsuario = new Usuario(nombreUsuario, pass);
    
    nuevoUsuario.setNombre(campoNombre.getText());
    nuevoUsuario.setApellido(campoApellido.getText());
    nuevoUsuario.setCorreo(campoCorreo.getText());
    

    I suppose it's a matter of style and / or order in the code. There are style manuals that expressly prohibit the use of constructors with more than 3 parameters ... and in other cases the builders are encouraged to be highly parameterized to "alleviate" inconsequential code.

    The important thing is to maintain the reusability, clarity, conciseness, principle of sole responsibility ... and all those good practices that enunciates the engineering of traditional software.

    In any case there are many more or less correct ways of doing it, being able to use more than one at a time. For example, using several constructors, and leaving setters for the attributes.

        
    answered by 17.10.2016 / 09:47
    source
    0

    Depends on the implementation that you are doing of your class, if you really require that it be a base field of your class, you must send it by the constructor.

    You can overload the constructor of the class if you wish and leave both, since the language allows it and gives you more freedom when it comes to continuing with the development.

        
    answered by 16.10.2016 в 06:17