lundi 20 février 2017

Transport text from textFields in a JPanel to facade object, and then build a new object

I'm trying to transport content from textFields in a JPanel to the application layer. And then build an object with this content in this layer.

Like a typical registration form...

In the application layer there are facade singleton classes, they call builders like this:

package domain;
public class ProjectBuilder {

   private final Project project;

   private ProjectBuilder() { 
      project = new Project();
   }

   public static ProjectBuilder project() {
       return new ProjectBuilder();
   }

   public ProjectBuilder withCode(int code) {
      project.setCode(code);
      return this;
   }

   public ProjectBuilder withBudget(int budget) {
      project.setBudget(budget);
      return this;
   }

   // more ProjectBuilder with...

   public Project build(){
      validate();
      return project;
   }
   private void validate(){
     // do all validations on the fire item itself and throw an exception if something fails
   }
}

public class Project {

    private static final AtomicLong countCode = new AtomicLong();
    private int code, budget;
    //more fields

    public static String getCountCode()
    {
        return String.valueOf(contCode.getAndIncrement());
    }

    public Project(){}

   // getters and setters
}

The facade singleton class calling to builder:

package aplication;

// << Singleton >>
public class FacadeProject{
// Singleton definition...

    public void newProject(final String[] fields){

        final int 
        code = Integer.parseInt(fields[0]), 
        budget = Integer.parseInt(fields[1]);   
        //more fields

        Project project = project() 
            .withCode(code)
            .withBudget(budget)
             //more .whith methods
            .build();
    }
    /...
}

The JPanel where String[] fields is created:

package presentation;

public class PanelNuevoProject extends javax.swing.JPanel {

    FacadeProject facade = FacadeProject.getInstance();

    private void btnNewProjectActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        final String[] fields = {
            txtCode.getText(),
            txtBudget.getText(),
           //more txt.getText(),
        };

        facade.newProject(fields);      
    //..
}

Relying on the order of an array does not seem flexible, does not even take advantage of a builder. But it was all I got.

Any good idea to make this flexible / reusable?

Thanks in advance for your help in my first question!

Aucun commentaire:

Enregistrer un commentaire