栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用策略模式结合InitializingBean或ApplicationContextAware简化if-else

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用策略模式结合InitializingBean或ApplicationContextAware简化if-else

前言

在我们平时开发业务过程中经常会遇到很多if-else的判断情况,需要根据不同的情况或者不同的参数走不同的逻辑,所以大量的if-else非常的麻烦,代码十分不美观,所以可以通过相关方法解决这个问题。

使用步骤

以生成文书为例,现在有一个根据不同的参数生成不同文书的需求

一、定义生成文书接口
package com.example.demo.simplifyIfElse;



public interface Document {
    
    Integer getType();
    
    void createDocument();
}
二、编写具体业务实现类 1、生成目录实现类
package com.example.demo.simplifyIfElse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class CatalogImpl implements Document {

    @Override
    public Integer getType() {
        return 2;
    }

    
    @Override
    public void createDocument() {
        log.info("执行生成目录具体业务类");
    }
}
2、生成封面实现类
package com.example.demo.simplifyIfElse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class CoverImpl implements Document {

    @Override
    public Integer getType() {
        return 1;
    }

    
    @Override
    public void createDocument() {
        log.info("执行生成封面具体业务类");
    }
}
三、编写策略类
package com.example.demo.simplifyIfElse;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;


@Slf4j
@Component
public class DocumentRouteInitialize implements InitializingBean {

    private final static EmptyDesc EMPTY = new EmptyDesc();

    private static final Map routerMap = Maps.newHashMap();

    @Autowired
    private List documentList;

    @Override
    public void afterPropertiesSet() throws Exception {
        for (Document service : documentList) {
            routerMap.put(service.getType(), service);
        }
    }
    public Document getService(Integer type) {
        Document document = routerMap.get(type);
        return Optional.ofNullable(document).orElse(EMPTY);
    }

    
    private static class EmptyDesc implements Document {
        @Override
        public Integer getType() {
            return null;
        }
        @Override
        public void createDocument() {
            log.error("类型错误,生成文书失败");
        }
    }
}

或者使用Sping容器提供的接口简化if-else。

package com.example.demo.simplifyIfElse;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;


@Slf4j
@Component
public class DocumentRouteApplicationContextAware implements ApplicationContextAware {

    private ApplicationContext applicationContext;


    private static final Map routerMap = Maps.newHashMap();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        // 准备 Document 的所有实现类
        prepareDocument();
    }


    private void prepareDocument() {
        // 获取容器中所有生成文书的实现类
        Map documentMap = this.applicationContext.getBeansOfType(Document.class);
        documentMap.forEach((key, value) -> {
            routerMap.put(value.getType(), value);
        });
    }

    public static Document getDocument(Integer type) {
        return routerMap.get(type);
    }

}
四、测试
@Autowired 
private DocumentRouteInitialize documentRouteInitialize;
@Test
public void simpleIfElse(){
    Document service = documentRouteInitialize.getService(1);
    service.createDocument();
    Document document = DocumentRouteApplicationContextAware.getDocument(2);
    document.createDocument();
}

效果:

参数可以自定义在配置文件中或者常量等形式更加灵活。

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号