springboot 使用 策略模式 去除过多的 if else 或 swich case!

一、前言

      最近写代码的过程中,发现业务逻辑很多的时候,总会有很多的 if ... else,很多的 if ...else,有的时候就会丢去某些逻辑。有的时候也会用到swich case 来区分不同类型下执行不同的方法。但是往往这种情况的时候,会显得一个方法的代码非常长。为了破解这样的嵌套。我们可以使用策略模式来解决。本篇小编将通过一个示例来破解一下。

二、提供一个需求

      我们这里有一个这样的需求,比如,现在有一个活动,活动根据分成了四个阶段:1-5日是预热活动,6-8日是正式活动,9-10是正式活动,11-以后是结束阶段。每个阶段要做的事情是不一样的。

      当然提供最简单的思路就是:

      可以通过 swich 或者if ... else 来根据不同的业务逻辑来写不同的业务代码,缺点是代码很长,不容易拓展,而且业务逻辑修改的时候会修改很多的地方。

       switch (SpringGiftStepEnum.getByValue(step)) {
            case PRE_ACTIVITY:
                //预热阶段
                break;
            case IN_ACTIVITY:
                //正式活动阶段
                break;
            case IN_TASK:
                //任务阶段
                break;
            default:
                //结束
                break;
       }

三、使用策略模式去除过多的 if else 或 swich case!

springboot 使用 策略模式 去除过多的 if else 或 swich case!_第1张图片

      小编处理的业务逻辑是这样的,在service中,通过调用handlerContext来获取抽象类AbstractHandler的实例。

      而项目再启动的时候,会扫描指定路径下带有我们自定义注解的类,并把类上的注解的值和类的实例作为k-v,放到handlerContext中。

springboot 使用 策略模式 去除过多的 if else 或 swich case!_第2张图片

HandlerProcessor,项目启动扫描handler入口

      HandlerProcessor:

      类中指明了扫描 com.soybean.notify.handler.biz 路径下的,带有HandlerType注解的类。

package com.soybean.notify.handler;


import com.google.common.collect.Maps;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@SuppressWarnings("unchecked")
public class HandlerProcessor implements BeanFactoryPostProcessor {

    private static final String HANDLER_PACKAGE = "com.soybean.notify.handler.biz";

    /**
     * 扫描@HandlerType,初始化HandlerContext,将其注册到spring容器
     *
     * @param beanFactory bean工厂
     * @see HandlerType
     * @see HandlerContext
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Map handlerMap = Maps.newHashMapWithExpectedSize(3);
        ClassScaner.scan(HANDLER_PACKAGE, HandlerType.class).forEach(clazz -> {
            String type = clazz.getAnnotation(HandlerType.class).value();
            handlerMap.put(type, clazz);
        });
        HandlerContext context = new HandlerContext(handlerMap);
        beanFactory.registerSingleton(HandlerContext.class.getName(), context);
    }

}

      HandlerType

      自定义注解

package com.soybean.notify.handler;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface HandlerType {

    String value();

}

      ClassScaner

      根据包路径和注解扫描类

package com.soybean.notify.handler;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.StringUtils;
import org.springframework.util.SystemPropertyUtils;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class ClassScaner implements ResourceLoaderAware {

    private final List includeFilters = new LinkedList();
    private final List excludeFilters = new LinkedList();

    private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);

    @SafeVarargs
    public static Set> scan(String[] basePackages, Class... annotations) {
        ClassScaner cs = new ClassScaner();

        if (ArrayUtils.isNotEmpty(annotations)) {
            for (Class anno : annotations) {
                cs.addIncludeFilter(new AnnotationTypeFilter(anno));
            }
        }

        Set> classes = new HashSet<>();
        for (String s : basePackages) {
            classes.addAll(cs.doScan(s));
        }

        return classes;
    }

    @SafeVarargs
    public static Set> scan(String basePackages, Class... annotations) {
        return ClassScaner.scan(StringUtils.tokenizeToStringArray(basePackages, ",; \t\n"), annotations);
    }

    public final ResourceLoader getResourceLoader() {
        return this.resourcePatternResolver;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourcePatternResolver = ResourcePatternUtils
                .getResourcePatternResolver(resourceLoader);
        this.metadataReaderFactory = new CachingMetadataReaderFactory(
                resourceLoader);
    }

    public void addIncludeFilter(TypeFilter includeFilter) {
        this.includeFilters.add(includeFilter);
    }

    public void addExcludeFilter(TypeFilter excludeFilter) {
        this.excludeFilters.add(0, excludeFilter);
    }

    public void resetFilters(boolean useDefaultFilters) {
        this.includeFilters.clear();
        this.excludeFilters.clear();
    }

    public Set> doScan(String basePackage) {
        Set> classes = new HashSet<>();
        try {
            String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + org.springframework.util.ClassUtils
                    .convertClassNameToResourcePath(SystemPropertyUtils
                            .resolvePlaceholders(basePackage))
                    + "/**/*.class";
            Resource[] resources = this.resourcePatternResolver
                    .getResources(packageSearchPath);

            for (int i = 0; i < resources.length; i++) {
                Resource resource = resources[i];
                if (resource.isReadable()) {
                    MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
                    if ((includeFilters.size() == 0 && excludeFilters.size() == 0) || matches(metadataReader)) {
                        try {
                            classes.add(Class.forName(metadataReader
                                    .getClassMetadata().getClassName()));
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException(
                    "I/O failure during classpath scanning", ex);
        }
        return classes;
    }

    protected boolean matches(MetadataReader metadataReader) throws IOException {
        for (TypeFilter tf : this.excludeFilters) {
            if (tf.match(metadataReader, this.metadataReaderFactory)) {
                return false;
            }
        }
        for (TypeFilter tf : this.includeFilters) {
            if (tf.match(metadataReader, this.metadataReaderFactory)) {
                return true;
            }
        }
        return false;
    }

}

      HandlerContext

      根据类型实例化抽象类

package com.soybean.notify.handler;

import java.util.Map;

/**
 * @Author: 
 * @Description: 处理器上下文,根据类型获取相应的处理器
 * @Date: Created in 10:07 2019/2/2
 */
@SuppressWarnings("unchecked")
public class HandlerContext {

    private Map handlerMap;

    public HandlerContext(Map handlerMap) {
        this.handlerMap = handlerMap;
    }

    public AbstractHandler getInstance(String type) {
        Class clazz = handlerMap.get(type);
        if (clazz == null) {
            throw new IllegalArgumentException("not found handler for type: " + type);
        }
        return (AbstractHandler) BeanTool.getBean(clazz);
    }

}

      BeanTool

package com.soybean.notify.handler;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class BeanTool implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if (applicationContext == null) {
            applicationContext = context;
        }
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static  T getBean(Class clazz) {
        return applicationContext.getBean(clazz);
    }

}

      AbstractHandler

      抽象方法类

package com.soybean.notify.handler;

import com.soybean.notify.bo.OrderBO;

public abstract class AbstractHandler {

    abstract public String handle(OrderBO bo);

}

      业务类1

package com.soybean.notify.handler.biz;


import com.soybean.notify.bo.OrderBO;
import com.soybean.notify.handler.AbstractHandler;
import com.soybean.notify.handler.HandlerType;
import org.springframework.stereotype.Component;


@Component
@HandlerType("1")
public class NormalHandler extends AbstractHandler {

    @Override
    public String handle(OrderBO bo) {
        return "处理普通订单";
    }

}

      业务类2

package com.soybean.notify.handler.biz;

import com.soybean.notify.bo.OrderBO;
import com.soybean.notify.handler.AbstractHandler;
import com.soybean.notify.handler.HandlerType;
import org.springframework.stereotype.Component;


@Component
@HandlerType("2")
public class GroupHandler extends AbstractHandler {

    @Override
    public String handle(OrderBO bo) {
        return "处理团购订单";
    }

}

      业务类3

package com.soybean.notify.handler.biz;


import com.soybean.notify.bo.OrderBO;
import com.soybean.notify.handler.AbstractHandler;
import com.soybean.notify.handler.HandlerType;
import org.springframework.stereotype.Component;


@Component
@HandlerType("3")
public class PromotionHandler extends AbstractHandler {

    @Override
    public String handle(OrderBO bo) {
        return "处理促销订单";
    }

}

四、小结

      策略模式的作用:就是把具体的算法实现从业务逻辑中剥离出来,成为一系列独立算法类,使得它们可以相互替换。

      在我们可以通过继续写心的业务逻辑类,来拓展我们的代码,这个是非常方便的,基本serviceImpl的类中是不用修改的。可以增加新的东西,不用修改旧的东西,这个正好是设计模式里面的开闭原则。

      在demo中,是通过项目开始的时候扫描指定包下的handler类,然后根据传入的type来从map中找出实例。

你可能感兴趣的:(------【大话设计模式】,------【Entity,Framework】,------【Spring,Boot】,------【Spring】)