Best approach (template?) For creating view objects from different resources

I am currently creating a view object from a search result (which comes from another separate resource) as follows:

ViewObject vo = searchResultToViewObjectMapper.map(searchResult); 

It works great.

However, now I want to add some pictures. These figures are a URL, and I can only determine their location through some other resource than the search result.

My first thought would be to use the Builder template, this would be:

 ViewObject vo = viewObjectBuilder.build(searchResult); 

and viewObjectBuilder will do something like this:

 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 good for this? Or are there other (better) ways? I am also wondering how the DDD approach will do this.

Thanks in advance!

+4
source share
1 answer

Using a factory will work for you if you have all the resources available before creating your object - just pass them to the factory method and it will do the magic.

If the created object (view) is created in steps - that is, at first you have only searchResult , after that you dig more and get additional URLs that are added to the view, and then you do some more to look for additional information, and only then you will want to get an object of the form, the builder is the best solution.

+1
source

Source: https://habr.com/ru/post/1382916/


All Articles