鱼皮聚合搜索
分别
适配器模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| interface SearchAdapter { Object search(String query); }
class ProductSearchAdapter implements SearchAdapter { public Object search(String query) { Object result = Elasticsearch.search(query, "products"); return result; } }
class ArticleSearchAdapter implements SearchAdapter { public Object search(String query) { Object result = Elasticsearch.search(query, "articles"); return result; } }
class UserSearchAdapter implements SearchAdapter { public Object search(String query) { Object result = Elasticsearch.search(query, "users"); return result; } }
class Search { private SearchAdapter adapter;
public Search(SearchAdapter adapter) { this.adapter = adapter; }
public Object search(String query) { Object result = adapter.search(query); return result; } }
Search search = new Search(new ProductSearchAdapter()); Object result = search.search("");
|
门面模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| interface SearchFacade { Object search(String query); }
class SearchFacadeImpl implements SearchFacade { private ProductSearchAdapter productSearch; private ArticleSearchAdapter articleSearch; private UserSearchAdapter userSearch;
public SearchFacadeImpl() { this.productSearch = new ProductSearchAdapter(); this.articleSearch = new ArticleSearchAdapter(); this.userSearch = new UserSearchAdapter(); }
public Object search(String query) { Object productResult = productSearch.search(query); Object articleResult = articleSearch.search(query); Object userResult = userSearch.search(query); Map<String, Object> result = new HashMap<>(); result.put("products", productResult); result.put("articles", articleResult); result.put("users", userResult); return result; } }
SearchFacade searchFacade = new SearchFacadeImpl(); Object result = searchFacade.search("iPhone");
|
在Java语言中,我们使用了相似的接口和类实现了适配器模式和门面模式,也顺利地通过封装实现了多种类型搜索的功能。
合并
好的,以下是适配器和门面方法结合起来使用的Java示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| interface SearchResult { Map<String, Object> search(String query); }
class ElasticsearchAdapter implements SearchResult { @Override public Map<String, Object> search(String query) { Map<String, Object> result = Elasticsearch.search(query, "products", "articles", "users"); return result; } }
class SearchFacade { private SearchResult searchResult;
public SearchFacade(SearchResult searchResult) { this.searchResult = searchResult; }
public Map<String, Object> search(String query) { Map<String, Object> result = searchResult.search(query); return result; } }
SearchResult searchResult = new ElasticsearchAdapter(); SearchFacade searchFacade = new SearchFacade(searchResult); Map<String, Object> result = searchFacade.search("iPhone");
|
在上面的代码中,我们首先定义了一个包含 search
方法的抽象接口 SearchResult
,表示搜索结果。然后我们定义了一个 ElasticsearchAdapter
类作为适配器,将 Elasticsearch 的 search
进行封装,以供门面模式调用。这里的 Elasticsearch.search
方法是一个假设的方法,用于演示适配器的作用,你可以将其替换成真实的搜索方法。
接着我们定义了 SearchFacade
类作为门面,其构造函数接受一个 SearchResult
实现,调用其 search
方法并返回结果。在客户端代码中,我们首先创建了 ElasticsearchAdapter 实例的引用并传给
SearchFacade构造函数,然后调用
search` 方法进行搜索。
注意,适配器模式的例子是有误的,原因在于接口中的 search
方法签名不一致,不符合重写规则。我们在上面的代码例子中更正了这个问题。