vendredi 8 février 2019

Java design pattern for object converters

I have this converter design that converts value, inside a main converter, there might be other converters.

public class VehicleConverter {

  @Inject
  private ConsumptionConverter consumptionConverter;

  @Inject
  private EmissionConverter emissionConverter;

  @Inject
  private CertificationConverter certificationConverter;

  //This vehicle is the source of information
  public VehicleModel convert(Vehicle vehicle){
     ConsumptionModel cm = consumptionConverter(vehicle.getConsumption());

     EmissionModel em = emissionConverter(vehicle.getEmission());

     CertificationModel certM = certificationConverterConverter(vehicle.getCertification());

     VehicleModel vm = new VehicleModel();
     vm.setConsumptionModel(vm);
     vm.setEmissionModel(em);
     vm.setCertificationModel(certM);

     return vm;
  }
}

Is there any better way that i can improve this codes by using design pattern, i went through most design pattern but what i can think of is by using Java interface

public interface GenericConverter<S, T>{

  public T convert(S source);
}

Aucun commentaire:

Enregistrer un commentaire