mercredi 12 juillet 2017

Call each implementation of some specific method that is defined in interface

I have a lot of implementations of specific interface. All those implementations are members of some general class. For instance:

interface Converter<DTO> {

DTO toDTO();

}

class General {

private SomeObject1 someObject1;
private SomeObject2 someObject2;
...

private List<SomeObject3> someObject3;
private List<SomeObject4> someObject4;

}

My goal is to build some functionality that will be able to call each implementation of toDTO method and build some map of those objects. For instance that map can looks like something like this:

Map<String, List<Object>> fieldNameToListOfDTOMap

I can implement some additional method within General class that would be able to build such map manualy. For instance:

public Map<String, List<Object>> buildDtoMap() {
    Map<String, List<Object>> dtoMap = new HashMap<>();

    dtoMap.put("dto1", Arrays.asList(this.someObject1.toDTO()));
    dtoMap.put("dto2", Arrays.asList(this.someObject2.toDTO()));

    dtoMap.put("dto3", this.someObject3.stream().map(DTO3:toDTO()).toList());
    dtoMap.put("dto3", this.someObject4.stream().map(DTO4:toDTO()).toList());
}

There are few questions:

  1. Is there any possibility to simplify this logic for buildDto map take into account that each members of General class implements same interface? I am thinking aboud iterator pattern but not sure that it is good idea for this case.
  2. What would be prefarable way to return such list of DTO objects take into account that each of DTO has different types and there is no possibility to change those DTOs?

Aucun commentaire:

Enregistrer un commentaire