日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 实现策略模式

發(fā)布時間:2024/9/16 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 实现策略模式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

擼了今年阿里、頭條和美團的面試,我有一個重要發(fā)現(xiàn).......>>>

需求

根據(jù)Restful API,前端傳給后臺的參數(shù),動態(tài)的調用具體的service。不用手動寫if else進行判斷。

service

UserService.java

package com.vincent.service;public interface UserService {String getGender(String gender); }

兩個service實現(xiàn)類分別實現(xiàn)這個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;} }

如何自動根據(jù)參數(shù)選擇具體的UserService實現(xiàn)類?

新建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;}}

根據(jù)bean的name來選擇。

工廠類FactoryStrategy負責創(chuàng)建策略的工廠,代碼比較簡單,比較關鍵的一點是AutoWired一個Map<String, UserService> 這個會在初始化的時候將所有的UserService自動加載到Map中,是不是很方便。使用concurrentHashMap是防止多線程操作的時候出現(xiàn)問題。

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";}}}

這樣就可以根據(jù)參數(shù)來選擇具體的service實現(xiàn)類了。

代碼:https://github.com/vincentduan/springboot-test

總結

以上是生活随笔為你收集整理的springboot 实现策略模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。