Spring中實(shí)現(xiàn)策略模式的幾種方式小結(jié)
一.背景
在寫業(yè)務(wù)代碼的時(shí)候,難免會(huì)遇到很多if-else,這個(gè)時(shí)候如果if-else不是很多可以用if-else。如果此時(shí)場(chǎng)景過多,太多的if-else會(huì)導(dǎo)致代碼比較臃腫,所以這個(gè)時(shí)候就需要抽象化,將每個(gè)場(chǎng)景獨(dú)立開來,定義一個(gè)頂層接口,不同場(chǎng)景有不同實(shí)現(xiàn),這個(gè)時(shí)候策略模式就出現(xiàn)了。本文主要闡述工作中常用的實(shí)現(xiàn)策略模式的幾種方式。
二.實(shí)現(xiàn)一
2.1 定義接口
package com.ljm.service; import com.ljm.constant.StrategyEnum; /** * @Author: ljm * @Date: 2024/4/29 15:09 * @Version: v1.0.0 * @Description: TODO **/ public interface IHandler { void handler(); StrategyEnum getHandleStrategy(); }
2.2 定義枚舉
package com.ljm.constant; /** * @Author: ljm * @Date: 2024/4/29 15:10 * @Version: v1.0.0 * @Description: TODO **/ public enum StrategyEnum { FIRST, SECOND, THIRD, FOUR }
2.3 定義實(shí)現(xiàn)類
import com.ljm.constant.StrategyEnum; import com.ljm.service.IHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class FirstHandler implements IHandler { @Override public void handler() { System.out.println("Execute First Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.FIRST; } }
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.IHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class SecondHandler implements IHandler { @Override public void handler() { System.out.println("Execute Second Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.SECOND; } }
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.IHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class ThirdHandler implements IHandler { @Override public void handler() { System.out.println("Execute Third Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.THIRD; } }
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.IHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class FourHandler implements IHandler { @Override public void handler() { System.out.println("Execute Four Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.FOUR; } }
2.4 定義策略工廠類
package com.ljm.service; import com.ljm.constant.StrategyEnum; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.concurrent.ConcurrentHashMap; /** * @Author: ljm * @Date: 2024/4/29 15:16 * @Version: v1.0.0 * @Description: TODO **/ @Service public class HandlerStrategyFactory { public final ConcurrentHashMap<StrategyEnum,IHandler> handlerStrategyMap = new ConcurrentHashMap<>(); @Autowired public HandlerStrategyFactory(List<IHandler> iHandlers){ iHandlers.forEach(x -> handlerStrategyMap.put(x.getHandleStrategy(),x)); } public IHandler getHandleStrategy(StrategyEnum strategyEnum){ return handlerStrategyMap.get(strategyEnum); } }
2.5 測(cè)試
package com.ljm; import com.MyApplication; import com.ljm.constant.StrategyEnum; import com.ljm.service.HandlerStrategyFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @Author: ljm * @Date: 2024/2/26 10:58 * @Version: v1.0.0 * @Description: TODO **/ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = MyApplication.class) public class MyTest { @Autowired private HandlerStrategyFactory handlerStrategyFactory; @Test public void testStrategy() { handlerStrategyFactory.getHandleStrategy(StrategyEnum.FIRST).handler(); handlerStrategyFactory.getHandleStrategy(StrategyEnum.SECOND).handler(); handlerStrategyFactory.getHandleStrategy(StrategyEnum.THIRD).handler(); handlerStrategyFactory.getHandleStrategy(StrategyEnum.FOUR).handler(); /* 測(cè)試結(jié)果 Execute First Handler Execute Second Handler Execute Third Handler Execute Four Handler*/ } }
實(shí)現(xiàn)一主要是為IHandler接口新增一個(gè)用于區(qū)分使用何種策略的方法,再構(gòu)建一個(gè)策略工廠類,利用構(gòu)造器注入的方式,將Spring中的實(shí)現(xiàn)類與相應(yīng)的枚舉類綁定,在使用的時(shí)候只需要傳入StrategyEnum相應(yīng)的值就可以調(diào)用想調(diào)用的策略.
三.實(shí)現(xiàn)二
3.1 定義抽象類
package com.ljm.service; import com.ljm.constant.StrategyEnum; import org.springframework.beans.factory.InitializingBean; /** * @Author: ljm * @Date: 2024/4/29 15:09 * @Version: v1.0.0 **/ public abstract class AbstractHandler implements InitializingBean { public abstract void handler(); public abstract StrategyEnum getHandleStrategy(); @Override public void afterPropertiesSet() throws Exception { HandlerStrategyFactory.registerHandlerStrategy(this); } }
3.2 定義枚舉
package com.ljm.constant; /** * @Author: ljm * @Date: 2024/4/29 15:10 * @Version: v1.0.0 * @Description: TODO **/ public enum StrategyEnum { FIRST, SECOND, THIRD, FOUR }
3.3 定義實(shí)現(xiàn)類
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.AbstractHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class FirstHandler extends AbstractHandler { @Override public void handler() { System.out.println("Execute First Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.FIRST; } }
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.AbstractHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class SecondHandler extends AbstractHandler { @Override public void handler() { System.out.println("Execute Second Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.SECOND; } }
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.AbstractHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class ThirdHandler extends AbstractHandler { @Override public void handler() { System.out.println("Execute Third Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.THIRD; } }
package com.ljm.service.impl; import com.ljm.constant.StrategyEnum; import com.ljm.service.AbstractHandler; import org.springframework.stereotype.Service; /** * @Author: ljm * @Date: 2024/4/29 15:12 * @Version: v1.0.0 * @Description: TODO **/ @Service public class FourHandler extends AbstractHandler { @Override public void handler() { System.out.println("Execute Four Handler"); } @Override public StrategyEnum getHandleStrategy() { return StrategyEnum.FOUR; } }
3.4 定義策略工廠類
package com.ljm.service; import com.ljm.constant.StrategyEnum; import java.util.concurrent.ConcurrentHashMap; /** * @Author: ljm * @Date: 2024/4/29 15:16 * @Version: v1.0.0 * @Description: TODO **/ public class HandlerStrategyFactory { public static final ConcurrentHashMap<StrategyEnum, AbstractHandler> handlerStrategyMap = new ConcurrentHashMap<>(); public static void registerHandlerStrategy(AbstractHandler handler) { handlerStrategyMap.put(handler.getHandleStrategy(), handler); } public static AbstractHandler getHandleStrategy(StrategyEnum strategyEnum) { return handlerStrategyMap.get(strategyEnum); } }
3.5 測(cè)試
package com.ljm; import com.MyApplication; import com.ljm.constant.StrategyEnum; import com.ljm.service.HandlerStrategyFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @Author: ljm * @Date: 2024/2/26 10:58 * @Version: v1.0.0 * @Description: TODO **/ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = MyApplication.class) public class MyTest { @Test public void testStrategy() { HandlerStrategyFactory.getHandleStrategy(StrategyEnum.FIRST).handler(); HandlerStrategyFactory.getHandleStrategy(StrategyEnum.SECOND).handler(); HandlerStrategyFactory.getHandleStrategy(StrategyEnum.THIRD).handler(); HandlerStrategyFactory.getHandleStrategy(StrategyEnum.FOUR).handler(); /* 測(cè)試結(jié)果 Execute First Handler Execute Second Handler Execute Third Handler Execute Four Handler*/ } }
實(shí)現(xiàn)二主要是為AbstractHandler抽象類實(shí)現(xiàn)InitializingBean接口,再對(duì)象初始化的時(shí)候?qū)⑵渥?cè)到我們自己構(gòu)建的策略工廠類中,此時(shí)的對(duì)象由Spring生成并與相應(yīng)的枚舉類綁定,在使用的時(shí)候只需要傳入StrategyEnum相應(yīng)的值就可以調(diào)用想調(diào)用的策略.
以上就是Spring中實(shí)現(xiàn)策略模式的幾種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Spring實(shí)現(xiàn)策略模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù)
本文主要介紹了springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03Spring?Cloud?Gateway集成Sentinel流控詳情
這篇文章主要介紹了Spring?Cloud?Gateway集成Sentinel流控詳情,Sentinel支持對(duì)Spring?Cloud?Gateway、Zuul等主流的API?Gateway進(jìn)行限流,需要的朋友可以參考一下2022-09-09JSON.toJSONString()空字段不忽略修改的問題
這篇文章主要介紹了JSON.toJSONString()空字段不忽略修改的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Spring boot+beetl+i18n國(guó)際化處理的方法
這篇文章主要介紹了Spring boot+beetl+i18n國(guó)際化處理的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04PowerJob的DesignateServer工作流程源碼解讀
這篇文章主要介紹了PowerJob的DesignateServer工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01java程序員必會(huì)的遠(yuǎn)程debug教程
這篇文章主要為大家介紹了java程序員必會(huì)的遠(yuǎn)程debug教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08