Java – the best way to create view objects from different resources (schema?)
At present, I am building a view object from search results (from different single resources), as follows:
ViewObject vo = searchResultToViewObjectMapper.map(searchResult);
It works well
But now I want to add some pictures These pictures are web addresses, and I can only determine their location through resources other than the source of search results
My first idea is to use builder pattern, which will become:
ViewObject vo = viewObjectBuilder.build(searchResult);
And viewobjectbuilder will do the following:
private SomeOtherResourceRepository someOtherResourceRepo; private SomeUrlBuilder someUrlBuilder; private SearchResultToViewObjectMapper searchResultToViewObjectMapper; public ViewObject build(SearchResult) { ViewObject vo = searchResultToViewObjectMapper.map(searchResult); String reference = someOtherResourceRepo.getOtherResource(searchResult); String urlToOtherResource = someUrlBuilder.build(reference); vo.setUrlToOtherResource(reference); return vo; }
The question is: is this a good way? Or is there another (better) way? I'm also curious about how DDD can do this
Thank you in advance!
Solution
If you have all the resources available before you create the object, using the factory will work for you - just pass them to the factory method and it will work
If the object (view) you are creating is created step by step - that is, first you only have SearchResult. After that, you can mine more and get some additional URLs. These URLs will be added to the view, and then you can search more for more information. The builder is a better solution only if you want to get the view object