還在用策略模式解決 if-else?Map+函數(shù)式接口方法才是YYDS!
上一篇:100多個免費常用API接口分享,調(diào)用完全不限次數(shù),以后總用得著!
來源:blog.csdn.net/qq_44384533/article/
details/109197926
本文介紹策略模式的具體應(yīng)用以及Map+函數(shù)式接口如何 “更完美” 的解決 if-else的問題。
實現(xiàn)方式:
根據(jù)優(yōu)惠券類型resourceType -> 確定查詢哪個數(shù)據(jù)表 根據(jù)編碼resourceId -> 到對應(yīng)的數(shù)據(jù)表里邊查詢優(yōu)惠券的派發(fā)方式grantType和領(lǐng)取規(guī)則
優(yōu)惠券有多種類型,分別對應(yīng)了不同的數(shù)據(jù)庫表:
實際的優(yōu)惠券遠(yuǎn)不止這些,這個需求是要我們寫一個業(yè)務(wù)分派的邏輯
switch(resourceType){
?case?"紅包":?
??查詢紅包的派發(fā)方式?
??break;
?case?"購物券":?
??查詢購物券的派發(fā)方式
??break;
?case?"QQ會員"?:
??break;
?case?"外賣會員"?:
??break;
?......
?default?:?logger.info("查找不到該優(yōu)惠券類型resourceType以及對應(yīng)的派發(fā)方式");
??break;
}
策略模式
以下是策略模式的具體結(jié)構(gòu)

switch(resourceType){
?case?"紅包":?
??String?grantType=new?Context(new?RedPaper()).ContextInterface();
??break;
?case?"購物券":?
??String?grantType=new?Context(new?Shopping()).ContextInterface();
??break;
?
?......
?default?:?logger.info("查找不到該優(yōu)惠券類型resourceType以及對應(yīng)的派發(fā)方式");
??break;
Map+函數(shù)式接口
需求:根據(jù)優(yōu)惠券(資源)類型resourceType和編碼resourceId查詢派發(fā)方式grantType
上代碼:
@Service
public?class?QueryGrantTypeService?{
?
????@Autowired
????private?GrantTypeSerive?grantTypeSerive;
????private?Map>?grantTypeMap=new?HashMap<>();
????/**
?????*??初始化業(yè)務(wù)分派邏輯,代替了if-else部分
?????*??key:?優(yōu)惠券類型
?????*??value:?lambda表達式,最終會獲得該優(yōu)惠券的發(fā)放方式
?????*/
????@PostConstruct
????public?void?dispatcherInit(){
????????grantTypeMap.put("紅包",resourceId->grantTypeSerive.redPaper(resourceId));
????????grantTypeMap.put("購物券",resourceId->grantTypeSerive.shopping(resourceId));
????????grantTypeMap.put("qq會員",resourceId->grantTypeSerive.QQVip(resourceId));
????}
?
????public?String?getResult(String?resourceType){
????????//Controller根據(jù)?優(yōu)惠券類型resourceType、編碼resourceId?去查詢?發(fā)放方式grantType
????????Function?result=getGrantTypeMap.get(resourceType);
????????if(result!=null){
?????????//傳入resourceId?執(zhí)行這段表達式獲得String型的grantType
????????????return?result.apply(resourceId);
????????}
????????return?"查詢不到該優(yōu)惠券的發(fā)放方式";
????}
}
如果單個 if 語句塊的業(yè)務(wù)邏輯有很多行的話,我們可以把這些 業(yè)務(wù)操作抽出來,寫成一個單獨的Service,即:
//具體的邏輯操作
@Service
public?class?GrantTypeSerive?{
????public?String?redPaper(String?resourceId){
????????//紅包的發(fā)放方式
????????return?"每周末9點發(fā)放";
????}
????public?String?shopping(String?resourceId){
????????//購物券的發(fā)放方式
????????return?"每周三9點發(fā)放";
????}
????public?String?QQVip(String?resourceId){
????????//qq會員的發(fā)放方式
????????return?"每周一0點開始秒殺";
????}
}
入?yún)tring resourceId是用來查數(shù)據(jù)庫的,這里簡化了,傳參之后不做處理。
@RestController
public?class?GrantTypeController?{
????@Autowired
????private?QueryGrantTypeService?queryGrantTypeService;
????@PostMapping("/grantType")
????public?String?test(String?resourceName){
????????return?queryGrantTypeService.getResult(resourceName);
????}
}

你的隊友得會lambda表達式才行啊,他不會讓他自己百度去
最后捋一捋本文講了什么
相關(guān)閱讀:2T架構(gòu)師學(xué)習(xí)資料干貨分享
全棧架構(gòu)社區(qū)交流群
?「全棧架構(gòu)社區(qū)」建立了讀者架構(gòu)師交流群,大家可以添加小編微信進行加群。歡迎有想法、樂于分享的朋友們一起交流學(xué)習(xí)。
看完本文有收獲?請轉(zhuǎn)發(fā)分享給更多人
往期資源:
評論
圖片
表情
