I am writing a library in java and I need to convert (map) one model to another and vice versa. What I did was implementing an abstract converter class with 2 generic types, containing convertTo and convertFrom methods, and extended my converters from it.
public abstract class TypeConverter<MyType, OtherLibType> {
private final Function<MyType, OtherLibType> fromMyType;
private final Function<OtherLibType, MyType> fromOtherLibType;
public TypeConverter(final Function<MyType, OtherLibType> fromMyType,
final Function<OtherLibType, MyType> fromOtherLibType
) {
this.fromMyType = fromMyType;
this.fromOtherLibType = fromOtherLibType;
}
public final OtherLibType convertFromMyType(final MyType myType) {
return fromMyType.apply(myType);
}
public final MyType convertFromJspritType(final OtherLibType otherLibType) {
return fromOtherLibType.apply(otherLibType);
}
}
But for specific implementations I also may need other objects for converting and I don't know what is the best practice to implement such things. I thought about adapter and facade but they don't seem to match my requirements. Is there a design pattern that I could use for this? Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire