I want to create few mappers using strategy pattern -all of the mappers produce XXXDTO object but consume entities that aren't exactly the same. Here is my interface:
public interface XXXDTOMapper<T> {
XXXDTO toXXXDTO(T source);
}
And here are my implemntations:
@Component
public class GoodXXXDTOMapper implements XXXDTOMapper<GoodDTO> {
@Override
public XXXDTO toXXXDTO(GoodDTO source) {
// some logic here
return XXXDDTO;
}
}
and:
@Component
public class BadXXXDTOMapper implements XXXDTOMapper<BadDTO> {
@Override
public XXXDTO toXXXDTO(BadDTO source) {
// some logic here
return XXXDDTO;
}
}
Is there a simple way to use this strategy pattern it in my XXXService class that aggregates those two types (GoodDTO and BadDTO) and produces XXXDTO? What I mean is:
@Service
public class XXXService {
private final XXXMapper xxxMapper;
@Autowired
public XXXService(XXXMapper xxxMapper) {
this.xxxMapper = xxxMapper;
}
public Stream<XXXDTO> aggregate(Stream<BadDTO> badStream, Stream<GoodDTO> goodStream) {
return Stream.concat(badStream.map(xxxMapper::toXXXDTO), goodStream.map(xxxMapper::toXXXDTO));
}
My code doesn't compile atm - saying there were 2 beans found - I understand why it doesn't work but looking for advice how to work around it so my strategy pattern does work. Do I need to implement factory pattern as well so at runtime it is decided which bean is used? How would you do it without all the ifs?
Aucun commentaire:
Enregistrer un commentaire