mardi 14 février 2017

Factory Pattern when the output type is different in java

I am fairly new to java and below are my mapper interface definitions:

public interface CourseMapper {
    Course map(JsonObject courseJson);
    List<Course> map(JsonArray coursesJson);
}

public interface LocationMapper {
    Location map(JsonObject locationJson);
    List<Location> map(JsonArray locationsJson);
}

and below are the two classes:

public interface Location {
    Long id();
    String name();
    Location mutate(Long id);
}

public interface Course {
    Long id();
    String name();
    Course mutate(Long id);
}

What I need is based on a key(String), I need to invoke the respective map() function of respective Mapper. Example if the key=="location", I need to invoke LocationMapper and if the key=="course", I need to invoke CourseMapper (for gson Mapper)...... and so on.

I tried using Factory Pattern for the same, but it seems that the same doesn't work when the Output is of different type. Would anyone know how to achieve the same without overtly complicating with if-else ladder.

Aucun commentaire:

Enregistrer un commentaire