lundi 27 décembre 2021

Using Factory Design Pattern in Spring

I organized the factory design pattern but I'm not sure if I used it right or wrong.

Here's my data model;

@Data
@AllArgsConstructor
@NoArgsConstructor
abstract public class Ga implements Serializable {

    private static final long serialVersionUID = -1977929156280284414L;

    private String id;
    private GaStaEnum gaSta;
    private PlaTuEnum plaTu;
    private BoaSiEnum boaSi;

}

@Data
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class XGa extends Ga {

    private static final long serialVersionUID = 3116827666531342601L;

    private Pla pla1;
    private Pla pla2;
    private int laSoPiIn;

.....

And my factory pattern is like this;

public class GaFactory {

    public static Ga createGa(GaType type) {
        Ga ga = null;

        if (X.equals(type)) {
            ga = new XGa();
        }

        return ga;
    }

}

I called the factory like this;

public XGaDTO init() {
    Ga xGa = GaFactory.createGa(X);
    repository.save((XGa) xGa);
    return mapper.toDTO((XGa) xGa);
}

Did I use it right? Or maybe I should add something more?

Thanks for advices.

Aucun commentaire:

Enregistrer un commentaire