springboot 实现策略模式
生活随笔
收集整理的這篇文章主要介紹了
springboot 实现策略模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
擼了今年阿里、頭條和美團的面試,我有一個重要發現.......>>>
需求
根據Restful API,前端傳給后臺的參數,動態的調用具體的service。不用手動寫if else進行判斷。
service
UserService.java
package com.vincent.service;public interface UserService {String getGender(String gender); }兩個service實現類分別實現這個UserService接口
FemaleServiceImpl.java
package com.vincent.service.impl;import com.vincent.service.UserService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service("female") public class FemaleServiceImpl implements UserService {@Autowiredprivate Client client;@Overridepublic String getGender(String name) {SearchResponse test = client.prepareSearch("test").setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", name))).setSize(0).get();System.out.println("female" + test.getHits().totalHits);return null;} }MaleServiceImpl.java
package com.vincent.service.impl;import com.vincent.service.UserService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service("male") public class MaleServiceImpl implements UserService {@Autowiredprivate Client client;@Overridepublic String getGender(String name) {SearchResponse test = client.prepareSearch("test").setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", name))).setSize(0).get();System.out.println("male:" + test.getHits().totalHits);return null;} }如何自動根據參數選擇具體的UserService實現類?
新建FactoryForStratge.java
package com.vincent.service.impl;import com.vincent.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.Map; import java.util.concurrent.ConcurrentHashMap;@Service public class FactoryForStrategy {@AutowiredMap<String, UserService> strategys = new ConcurrentHashMap<>(3);public UserService getStrategy(String component) throws Exception{UserService strategy = strategys.get(component);if(strategy == null) {throw new RuntimeException("no strategy defined");}return strategy;}}根據bean的name來選擇。
工廠類FactoryStrategy負責創建策略的工廠,代碼比較簡單,比較關鍵的一點是AutoWired一個Map<String, UserService> 這個會在初始化的時候將所有的UserService自動加載到Map中,是不是很方便。使用concurrentHashMap是防止多線程操作的時候出現問題。
UserController.java
package com.vincent.controller;import com.vincent.service.UserService; import com.vincent.service.impl.FactoryForStrategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;@RestController public class UserController {@AutowiredFactoryForStrategy factoryForStrategy;@GetMapping("/gender2/{gender}/{name}")public String getGender2(@PathVariable String gender, @PathVariable String name) {try {UserService strategy = factoryForStrategy.getStrategy(gender);String gender1 = strategy.getGender(name);return "success";} catch (Exception e) {e.printStackTrace();return "failed";}}}這樣就可以根據參數來選擇具體的service實現類了。
代碼:https://github.com/vincentduan/springboot-test
總結
以上是生活随笔為你收集整理的springboot 实现策略模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zookeeper教程
- 下一篇: HandlerInterceptorAd