jeudi 29 janvier 2015

What is the better design approach to convert object A to object B?

I would like to design an Interface which will be used for converting Object A to Object B. Something like below snippet -



Interface IConverter<S, T> {
T convertTo(S objectA);
}


But sometimes more external input parameters are needed to get the Object B. Sample interface is as below -



Interface IConverter<S, T> {
T convertTo(S objectA, Map<String, Object> params);
}


Now with this 2nd approach, when there are no extra parameters are needed, it is a kind of overhead to pass "null" to second argument. When these extra parameters are passed via Map, we loose compile time Type safety. What is the better way to handle this scenario?


One way is to keep these extra parameters as class level attribute in each implementation and inject it via constructor. But this definitely introduces state management in converter classes.


Aucun commentaire:

Enregistrer un commentaire