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相应的值就可以调用想调用的策略.

相关推荐
P.H. Infinity33 分钟前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天36 分钟前
java的threadlocal为何内存泄漏
java
caridle1 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^1 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋31 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端1 小时前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan1 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈1 小时前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫