Spring中实现策略模式的几种方式

Spring中实现策略模式的几种方式

一.背景

在写业务代码的时候,难免会遇到很多if-else,这个时候如果if-else不是很多可以用if-else。如果此时场景过多,太多的if-else会导致代码比较臃肿,所以这个时候就需要抽象化,将每个场景独立开来,定义一个顶层接口,不同场景有不同实现,这个时候策略模式就出现了。本文主要阐述工作中常用的实现策略模式的几种方式。

二.实现一

2.1 定义接口

java 复制代码
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 定义枚举

java 复制代码
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 定义实现类

java 复制代码
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;
    }
}
java 复制代码
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;
    }
}
java 复制代码
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;
    }
}
java 复制代码
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 定义策略工厂类

java 复制代码
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 测试

java 复制代码
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();

       /* 测试结果
        Execute First Handler
        Execute Second Handler
        Execute Third Handler
        Execute Four Handler*/
    }

}

实现一主要是为IHandler接口新增一个用于区分使用何种策略的方法,再构建一个策略工厂类,利用构造器注入的方式,将Spring中的实现类与相应的枚举类绑定,在使用的时候只需要传入StrategyEnum相应的值就可以调用想调用的策略.

三.实现二

3.1 定义抽象类

java 复制代码
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 定义枚举

java 复制代码
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 定义实现类

java 复制代码
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;
    }
}
java 复制代码
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;
    }
}
java 复制代码
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;
    }
}
java 复制代码
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 定义策略工厂类

java 复制代码
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 测试

java 复制代码
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();

       /* 测试结果
        Execute First Handler
        Execute Second Handler
        Execute Third Handler
        Execute Four Handler*/
    }

}

实现二主要是为AbstractHandler抽象类实现InitializingBean接口,再对象初始化的时候将其注册到我们自己构建的策略工厂类中,此时的对象由Spring生成并与相应的枚举类绑定,在使用的时候只需要传入StrategyEnum相应的值就可以调用想调用的策略.

相关推荐
drebander7 分钟前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
乌啼霜满天24911 分钟前
Spring 与 Spring MVC 与 Spring Boot三者之间的区别与联系
java·spring boot·spring·mvc
tangliang_cn16 分钟前
java入门 自定义springboot starter
java·开发语言·spring boot
程序猿阿伟17 分钟前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
Grey_fantasy26 分钟前
高级编程之结构化代码
java·spring boot·spring cloud
弗锐土豆33 分钟前
工业生产安全-安全帽第二篇-用java语言看看opencv实现的目标检测使用过程
java·opencv·安全·检测·面部
Elaine20239134 分钟前
零碎04 MybatisPlus自定义模版生成代码
java·spring·mybatis
小小大侠客1 小时前
IText创建加盖公章的pdf文件并生成压缩文件
java·pdf·itext
一二小选手1 小时前
【MyBatis】全局配置文件—mybatis.xml 创建xml模板
xml·java·mybatis
猿java1 小时前
Linux Shell和Shell脚本详解!
java·linux·shell