springboot工厂模式+策略模式消除if-else

简介: springboot工厂模式+策略模式消除if-else


  • 策略接口
/**
 * @author micky
 * @date 2021/11/19
 * 策略接口
 */
public interface DeviceRegisterStrategy {
    Object callIoDevice(xxxx);
}
  • 定义策略实现类
@Component
public class FirstRegStrategy implements DeviceRegisterStrategy {
    @Override
    public Object callIoDevice(xxxxx){
          doSomeThing();    //自定义策略逻辑
    }
}    
@Component
public class RecoverRegStrategy implements DeviceRegisterStrategy {
    @Override
    public Object callIoDevice(xxxx) {
        doSomeThing();  // 自定义策略逻辑
    }
}
  • 工厂类
/**
 * @author micky
 * @since 2021/11/19
 */
@Component
public class DeviceRegisterStrategyFactory {
    private static final Map<String, DeviceRegisterStrategy> map = new HashMap<>(2);
    @Autowired
    private FirstRegStrategy firstRegStrategy;
    @Autowired
    private RecoverRegStrategy recoverRegStrategy;
    @PostConstruct
    public void initDeviceRegisterStrategy(){
        map.put("FirstRegister",firstRegStrategy);
        map.put("RecoverRegister",recoverRegStrategy);
    }
    public static DeviceRegisterStrategy getDeviceRegisterStrategy(String name) {
        return map.get(name);
    }
}

为了便于管理 ,name 也可以定义枚举类

package com.xhwl.smarthome.pojo.enums;
/**
 * @author wangxinyu
 * * @date 2021/11/19
 */
public enum RegisterEnum {
    FIRST_REG(1,"FirstRegister"),
    RECOVER_REG(2,"RecoverRegister");
    private int type;
    private String name;
    public int getType() {
        return type;
    }
    public String getName() {
        return name;
    }
    RegisterEnum(int type, String name) {
        this.type = type;
        this.name = name;
    }
    RegisterEnum(){}
    public static RegisterEnum of(int type){
        for (RegisterEnum e : RegisterEnum.values()) {
            if(e.getType() == type){
                return e;
            }
        }
        return null;
    }
}
  • 测试
@Autowired
private DeviceRegisterStrategyFactory strategyFactory;
@PostMapping("/test")
public void initializationInv(@RequestParam(value = "name") String name){
    strategyFactory.getDeviceRegisterStrategy(name).callIoDevice();
}


相关文章
|
设计模式 算法 Java
Springboot 使用设计模式- 策略模式
Springboot 使用设计模式- 策略模式
1127 0
Springboot 使用设计模式- 策略模式
|
Java Spring
Spring Boot使用策略模式指定Service实现类
Spring Boot使用策略模式指定Service实现类
317 0
|
设计模式 算法 Java
Spring Boot 项目怎么使用策略模式?
策略模式是一种设计模式,它允许在运行时选择不同的算法或行为。此模式通过定义一系列算法并将它们封装在独立的类中实现,这些类可以互相替换。这样可以根据不同情况动态选择最适合的算法。 在Spring框架中,可以通过依赖注入来实现策略模式。首先定义一个抽象策略类(接口或抽象类),然后创建具体策略类实现不同的算法。具体策略类通过`@Service`注解并在名称中指定特定的策略(如加法、减法等)。在上下文类(如Service类)中,通过`@Resource`注入策略对象的Map集合,根据需要选择并执行相应的策略。
667 0
|
设计模式 Java 开发者
谈谈springboot的工厂模式
【4月更文挑战第13天】Spring Boot中的工厂模式是一种用于解耦组件创建过程的设计模式,它允许系统在运行时根据需要动态地创建不同类型的对象。这种模式在Spring框架中得到了广泛的应用,特别是在依赖注入(DI)和控制反转(IoC)的上下文中,它有助于管理复杂的依赖关系并提高代码的可维护性和可扩展性
518 7
|
设计模式 Java
【SpringBoot】如何使用策略模式+抽象工厂+反射
【SpringBoot】如何使用策略模式+抽象工厂+反射
397 2
|
设计模式 算法 Java
谈谈springboot的策略模式
【4月更文挑战第14天】Spring Boot 中的策略模式主要通过接口和多个实现类来实现,这种设计允许在运行时动态选择算法或行为的具体实现。这是一种非常灵活的设计模式,可以帮助解耦组件之间的关系,使得系统更加灵活并易于扩展和维护。
310 2
|
设计模式 算法 Java
利用springboot初始化机制三种实现策略模式的应用
面试时总被问,spring中使用了哪些设计模式,你在实际开发中又使用哪些设计模式。给他手指一个方向跟他说,这就是一个模式:go out!。
733 0
|
设计模式 算法 安全
SpringBoot下的策略模式,消灭了大量的ifelse,真香!
SpringBoot下的策略模式,消灭了大量的ifelse,真香!
398 0
SpringBoot下的策略模式,消灭了大量的ifelse,真香!
|
1月前
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。