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 使用设计模式- 策略模式
1089 0
Springboot 使用设计模式- 策略模式
|
Java Spring
Spring Boot使用策略模式指定Service实现类
Spring Boot使用策略模式指定Service实现类
287 0
|
设计模式 算法 Java
Spring Boot 项目怎么使用策略模式?
策略模式是一种设计模式,它允许在运行时选择不同的算法或行为。此模式通过定义一系列算法并将它们封装在独立的类中实现,这些类可以互相替换。这样可以根据不同情况动态选择最适合的算法。 在Spring框架中,可以通过依赖注入来实现策略模式。首先定义一个抽象策略类(接口或抽象类),然后创建具体策略类实现不同的算法。具体策略类通过`@Service`注解并在名称中指定特定的策略(如加法、减法等)。在上下文类(如Service类)中,通过`@Resource`注入策略对象的Map集合,根据需要选择并执行相应的策略。
543 0
|
设计模式 Java 开发者
谈谈springboot的工厂模式
【4月更文挑战第13天】Spring Boot中的工厂模式是一种用于解耦组件创建过程的设计模式,它允许系统在运行时根据需要动态地创建不同类型的对象。这种模式在Spring框架中得到了广泛的应用,特别是在依赖注入(DI)和控制反转(IoC)的上下文中,它有助于管理复杂的依赖关系并提高代码的可维护性和可扩展性
469 7
|
设计模式 Java
【SpringBoot】如何使用策略模式+抽象工厂+反射
【SpringBoot】如何使用策略模式+抽象工厂+反射
373 2
|
设计模式 算法 Java
谈谈springboot的策略模式
【4月更文挑战第14天】Spring Boot 中的策略模式主要通过接口和多个实现类来实现,这种设计允许在运行时动态选择算法或行为的具体实现。这是一种非常灵活的设计模式,可以帮助解耦组件之间的关系,使得系统更加灵活并易于扩展和维护。
272 2
|
设计模式 算法 Java
利用springboot初始化机制三种实现策略模式的应用
面试时总被问,spring中使用了哪些设计模式,你在实际开发中又使用哪些设计模式。给他手指一个方向跟他说,这就是一个模式:go out!。
697 0
|
设计模式 算法 安全
SpringBoot下的策略模式,消灭了大量的ifelse,真香!
SpringBoot下的策略模式,消灭了大量的ifelse,真香!
377 0
SpringBoot下的策略模式,消灭了大量的ifelse,真香!
|
18天前
|
前端开发 安全 Java
基于springboot+vue开发的会议预约管理系统
一个完整的会议预约管理系统,包含前端用户界面、管理后台和后端API服务。 ### 后端 - **框架**: Spring Boot 2.7.18 - **数据库**: MySQL 5.6+ - **ORM**: MyBatis Plus 3.5.3.1 - **安全**: Spring Security + JWT - **Java版本**: Java 11 ### 前端 - **框架**: Vue 3.3.4 - **UI组件**: Element Plus 2.3.8 - **构建工具**: Vite 4.4.5 - **状态管理**: Pinia 2.1.6 - **HTTP客户端
120 4
基于springboot+vue开发的会议预约管理系统
|
5月前
|
JavaScript 前端开发 Java
制造业ERP源码,工厂ERP管理系统,前端框架:Vue,后端框架:SpringBoot
这是一套基于SpringBoot+Vue技术栈开发的ERP企业管理系统,采用Java语言与vscode工具。系统涵盖采购/销售、出入库、生产、品质管理等功能,整合客户与供应商数据,支持在线协同和业务全流程管控。同时提供主数据管理、权限控制、工作流审批、报表自定义及打印、在线报表开发和自定义表单功能,助力企业实现高效自动化管理,并通过UniAPP实现移动端支持,满足多场景应用需求。
456 1