dimanche 5 mars 2017

Design data class in Java

I researched a bit and i'm not sure about this.

I have a proccess that need returns a bunch of data. I put the data in a Result class:

public final class Result implements Serializable {
/** Inmmutable class
     * 
     */
    private static final long serialVersionUID = -916675731077365794L;

    private final String nick;
    private final LocalDateTime date_init;
    private final LocalDateTime date_end;

    private final double profit;
    private final double tax;

    private final Double balance;

    private final int inputs;

    private final List<CustomObjects> inputs_by_month;

    // Keep going...

    // Mother fucking constructor

    Results(String nick, LocalDateTime date_init, LocalDateTime date_end,
            double profit, double tax/* Keep going... */) {
        super();
        this.nick = nick;
        this.date_init = date_init;
        this.date_end = date_end;
        this.profit = profit;
        this.tax = tax;
        /* Keep going... */
    }

    /* getters here */
}

I'm not sure how i could create this object. Yeah, i could use a Factory class

public final class ResultFactory {

    private String nick;
    private LocalDateTime date_init;
    private LocalDateTime date_end;

    private double profit;
    private double tax;

    private Double balance;

    private int inputs;

    private List<CustomObjects> inputs_by_month;

    // Keep going...

    public Result getResult()
    {
       return Result(nick,date_init,date_end,/*...*/);
    }

    public void setNick(String nick)
    {
        this.nick = nick;
    }

    public void setDateInit(LocalDateTime date_init){
        this.date_init = date_init;
    }

    /* ... */
}

But i am duplicating the entire structure. It doesn't look right.

I'm looking for a pattern for store data in a inmutable class.

Aucun commentaire:

Enregistrer un commentaire