Spring中實現(xiàn)策略模式的幾種方式小結(jié)
一.背景
在寫業(yè)務(wù)代碼的時候,難免會遇到很多if-else,這個時候如果if-else不是很多可以用if-else。如果此時場景過多,太多的if-else會導(dǎo)致代碼比較臃腫,所以這個時候就需要抽象化,將每個場景獨立開來,定義一個頂層接口,不同場景有不同實現(xiàn),這個時候策略模式就出現(xiàn)了。本文主要闡述工作中常用的實現(xiàn)策略模式的幾種方式。
二.實現(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 定義實現(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 測試
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();
/* 測試結(jié)果
Execute First Handler
Execute Second Handler
Execute Third Handler
Execute Four Handler*/
}
}
實現(xiàn)一主要是為IHandler接口新增一個用于區(qū)分使用何種策略的方法,再構(gòu)建一個策略工廠類,利用構(gòu)造器注入的方式,將Spring中的實現(xiàn)類與相應(yīng)的枚舉類綁定,在使用的時候只需要傳入StrategyEnum相應(yīng)的值就可以調(diào)用想調(diào)用的策略.
三.實現(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 定義實現(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 測試
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();
/* 測試結(jié)果
Execute First Handler
Execute Second Handler
Execute Third Handler
Execute Four Handler*/
}
}
實現(xiàn)二主要是為AbstractHandler抽象類實現(xiàn)InitializingBean接口,再對象初始化的時候?qū)⑵渥缘轿覀冏约簶?gòu)建的策略工廠類中,此時的對象由Spring生成并與相應(yīng)的枚舉類綁定,在使用的時候只需要傳入StrategyEnum相應(yīng)的值就可以調(diào)用想調(diào)用的策略.
以上就是Spring中實現(xiàn)策略模式的幾種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Spring實現(xiàn)策略模式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot3整合Druid數(shù)據(jù)源的實現(xiàn)過程
文章介紹了一個基于Spring?Boot?3的程序?qū)崿F(xiàn)過程,包括創(chuàng)建項目、引入依賴、編寫啟動類、配置文件、Controller、啟動測試以及問題解決,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-11-11
基于hibernate框架在eclipse下的配置方法(必看篇)
下面小編就為大家?guī)硪黄趆ibernate框架在eclipse下的配置方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java結(jié)合Vue項目打包并進(jìn)行服務(wù)器部署
本文主要介紹了Java結(jié)合Vue項目打包并進(jìn)行服務(wù)器部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringBoot快速實現(xiàn)IP地址解析的全攻略
在當(dāng)今的互聯(lián)網(wǎng)應(yīng)用中,IP地址解析已成為許多系統(tǒng)不可或缺的功能,這篇文章主要為大家詳細(xì)介紹了如何使用SpringBoot快速實現(xiàn)IP地址解析,有需要的小伙伴可以了解下2026-02-02

