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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>