Springboot中實(shí)現(xiàn)策略模式+工廠模式的方法
策略模式和工廠模式相信大家都比較熟悉,但是大家有沒有在springboot中實(shí)現(xiàn)策略和工廠模式?
具體策略模式和工廠模式的UML我就不給出來了,使用這個(gè)這兩個(gè)模式主要是防止程序中出現(xiàn)大量的IF ELSE IF ELSE....。接下來咱們直接實(shí)現(xiàn),項(xiàng)目結(jié)構(gòu)圖:

工廠類FactoryStrategy負(fù)責(zé)創(chuàng)建策略的工廠,代碼比較簡(jiǎn)單,比較關(guān)鍵的一點(diǎn)是AutoWired一個(gè)Map<String, Strategy> 這個(gè)會(huì)在初始化的時(shí)候?qū)⑺械腟trategy自動(dòng)加載到Map中,是不是很方便。使用concurrentHashMap是防止多線程操作的時(shí)候出現(xiàn)問題。同時(shí)還要注意@Service注解。
package com.hqs.pattern.factory;
import com.hqs.pattern.strategy.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author huangqingshi
* @Date 2019-01-31
*/
@Service
public class FactoryForStrategy {
@Autowired
Map<String, Strategy> strategys = new ConcurrentHashMap<>(3);
public Strategy getStrategy(String component) throws Exception{
Strategy strategy = strategys.get(component);
if(strategy == null) {
throw new RuntimeException("no strategy defined");
}
return strategy;
}
}接下來就是Strategy接口,就一個(gè)doOperation方法。
package com.hqs.pattern.strategy;
/**
* @author huangqingshi
* @Date 2019-01-31
*/
public interface Strategy {
String doOperation();
}定義接口的實(shí)現(xiàn),我定義了三個(gè), 都類似,這里我就放出一個(gè)來吧。Component里邊的one是指定其名字,這個(gè)會(huì)作為key放到Map strategys里邊。
package com.hqs.pattern.strategy.impl;
import com.hqs.pattern.strategy.Strategy;
import org.springframework.stereotype.Component;
/**
* @author huangqingshi
* @Date 2019-01-31
*/
@Component("one")
public class StrategyOne implements Strategy {
@Override
public String doOperation() {
return "one";
}
}好了,寫一個(gè)Controller類,用于進(jìn)行測(cè)試,當(dāng)然我還是使用swagger,使用swagger的時(shí)候有個(gè)細(xì)節(jié),就是注意生產(chǎn)上一定不能打開,否則是個(gè)非??膳碌氖虑?。
package com.hqs.pattern.controller;
import com.hqs.pattern.factory.FactoryForStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author huangqingshi
* @Date 2019-01-31
*/
@Controller
public class StrategyController {
@Autowired
FactoryForStrategy factoryForStrategy;
@PostMapping("/strategy")
@ResponseBody
public String strategy(@RequestParam("key") String key) {
String result;
try {
result = factoryForStrategy.getStrategy(key).doOperation();
} catch (Exception e) {
result = e.getMessage();
}
return result;
}
}打開swagger進(jìn)行測(cè)試,輸入one,返回one。輸入four,返回no strategy defined。后續(xù)如果有新策略的話,直接實(shí)現(xiàn)即可。


好了,這塊就這么完成了,你get到了嗎?
代碼地址:https://github.com/stonehqs/pattern.git
到此這篇關(guān)于Springboot中實(shí)現(xiàn)策略模式+工廠模式的文章就介紹到這了,更多相關(guān)Springboot策略模式工廠模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在mybatis中使用mapper進(jìn)行if條件判斷
這篇文章主要介紹了在mybatis中使用mapper進(jìn)行if條件判斷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Spring強(qiáng)大事務(wù)兼容數(shù)據(jù)庫多種組合解決業(yè)務(wù)需求
這篇文章主要為大家介紹了Spring強(qiáng)大事務(wù)兼容數(shù)據(jù)庫多種組合解決業(yè)務(wù)需求示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
java發(fā)送HttpClient請(qǐng)求及接收請(qǐng)求結(jié)果過程的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨ava發(fā)送HttpClient請(qǐng)求及接收請(qǐng)求結(jié)果過程的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Java使用snmp協(xié)議實(shí)現(xiàn)采集服務(wù)器信息
SNMP是一種用于管理網(wǎng)絡(luò)設(shè)備的協(xié)議,它是一種標(biāo)準(zhǔn)化的協(xié)議,被用于監(jiān)控和管理網(wǎng)絡(luò)設(shè)備,這篇文章主要介紹了Java如何使用snmp協(xié)議采集服務(wù)器信息,需要的可以參考下2024-12-12
Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解
這篇文章主要介紹了Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
AsyncHttpClient?ClientStats源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient?ClientStats源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java實(shí)現(xiàn)的圖片上傳工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖片上傳工具類,涉及java針對(duì)圖片文件的檢查、上傳、清除等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10

