美文网首页
策略模式

策略模式

作者: 给代码点颜色 | 来源:发表于2023-07-09 18:12 被阅读0次

写在前面的话

很多的系统在不断迭代的过程中,免不了代码的默认规范越来越不可控,越来越堆积,这里希望近一点绵薄之力,希望既能相对规范下代码的逻辑,又可以方便后来者更好的融入前人的规范中,并轻松的进行扩展和优化。

注:
相关系列文章会不定期更新(主要因为我比较懒),有想法的读者可以留言探讨哈~

另外,如有引用或者拷贝的请注明出处。

废话不多说了,下面进入新的废话中~

策略模式

不想说话,只想上代码

还是先说下基本思路吧~

同样延续一套模板到处使用的思路,那么每一类事件需要有统一的范围(IScope),然后实现此类事件的策略时也需要指定此策略的范围及具体是哪个策略(这里定义了注解StrategyScope,需要在具体的策略类上加上)。

1、先定义一套策略模板接口

泛型T代表事件类型,I代表触发事件(入参),O代表出参,也就策略方法返回值。

基于事件的策略范围接口定义IScope
public interface IScope<T extends Enum<T> & IScope<T>> {
    /**
     * 这个实际用处不大,主要限制子类必须是枚举
     */
    T scope();

    /**
     * 这个工具方法,用来把注解中的策略name转化为对应的IScope对象
     */
    static <T extends Enum<T> & IScope<T>> IScope<T> from(String name, Class<T> type) {
        if (name == null) {
            return null;
        }

        T[] constants = type.getEnumConstants();
        for (T t : constants) {
            if (t.name().equals(name)) {
                return t;
            }
        }
        return null;
    }
}
策略生效范围的注解StrategyScope
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StrategyScope {

   /**
    * 实现了IScope的枚举类
    */
   Class<? extends IScope> scope();

   /**
    * 策略支持的枚举.name()列表
    */
   String[] names();
}
策略接口IStrategy<I, O>
    /**
     * 执行具体策略
     */
    O process(I in) throws Exception;

    /**
     * 策略异常处理
     */
    O exceptionCaught(Throwable cause, I in) throws Exception;
策略工厂IStrategyFactory
public interface IStrategyFactory<T extends Enum<T> & IScope<T>, I, O> {

    /**
     * 注册策略范围及对应的策略类列表
     */
    IStrategyFactory<T, I, O> register(IScope<T> scope, IStrategy<I, O>... strategys);

    /**
     * 执行策略
     */
    O process(IScope scope, I in);

    /**
     * 策略异常处理,优先级低于IStrategy.exceptionCaught
     */
    O exceptionCaught(Throwable cause, I in);
}
躲不开的通用调度器IStrategyDispatcher
public interface IStrategyDispatcher {

    /**
     * 这里可以注册不同范围的策略
     */
    <T extends Enum<T> & IScope<T>, I, O> IStrategyDispatcher register(IStrategy<I, O>... strategys);

    /**
     * 执行策略
     */
    <I, O> O process(IScope scope, I in);
}

2、基于策略模板的默认实现

假设事件范围为ScopeTest

同一套的事件策略,需要定义自己的IScope,这里的仅供参考。

public enum ScopeTest implements IScope {

    T1,

    T2;

    @Override
    public Enum scope() {
        return this;
    }

}
策略接口IStrategy<I, O>

这里只实现个抽象策略类,具体策略需要自行定义。

public abstract class AbstractStrategy<I, O> implements IStrategy<I, O> {

    @Override
    public O exceptionCaught(Throwable cause, I in) throws Exception {
        throw (Exception) cause;
    }
}

例如:

@StrategyScope(scope = ScopeTest.class, names = "T1")
public class ScopeStrategyTest extends AbstractStrategy<String, String> {

    @Override
    public String process(String in) throws Exception {
        return "hey " + in;
    }
}

注解中的T1为ScopeTest中的枚举值。

策略工厂IStrategyFactory
@Slf4j
public class DefaultStrategyFactory<T extends Enum<T> & IScope<T>, I, O> implements IStrategyFactory<T, I, O> {

    private Map<IScope, IStrategy<I, O>> strategyMap = new HashMap<>();

    @Override
    public IStrategyFactory<T, I, O> register(IScope<T> scope, IStrategy<I, O>... strategys) {
        for (IStrategy<I, O> strategy : strategys) {
            strategyMap.put(scope, strategy);
        }
        return this;
    }

    @Override
    public O process(IScope scope, I in) {
        IStrategy<I, O> strategy = strategyMap.get(scope);
        if (strategy == null) {
            log.error("[process] no strategy. scope={}, in={}", scope, in);
            throw new RuntimeException("un support strategy");
        }

        try {
            return strategy.process(in);
        } catch (Exception ex) {
            try {
                return strategy.exceptionCaught(ex, in);
            } catch (Exception e) {
                return exceptionCaught(e, in);
            }
        }
    }

    @Override
    public O exceptionCaught(Throwable cause, I in) {
        log.error("[exceptionCaught] exec failed. in={}", in, cause);
        return null;
    }

}
通用策略调度器IStrategyDispatcher
@Slf4j
public class DefaultStrategyDispatcher implements IStrategyDispatcher {

    private Map<Class<? extends IScope>, IStrategyFactory> factoryMap = new HashMap<>();

    @Override
    public <T extends Enum<T> & IScope<T>, I, O> IStrategyDispatcher register(IStrategy<I, O>... strategys) {
        for (IStrategy<I, O> strategy : strategys) {
            StrategyScope scope = strategy.getClass().getDeclaredAnnotation(StrategyScope.class);
            if (scope != null) {
                Class<T> clazz = (Class<T>) scope.scope();

                IStrategyFactory factory = factoryMap.get(clazz);
                if (factory == null) {
                    factory = new DefaultStrategyFactory();
                    factoryMap.put(clazz, factory);
                }

                String[] names = scope.names();
                for (String name : names) {
                    factory.register(IScope.from(name, clazz), strategy);
                }
            } else {
                log.error("[register] no StrategyScope. strategy={}", strategy);
                throw new RuntimeException("un support strategy");
            }
        }
        return this;
    }

    @Override
    public <I, O> O process(IScope scope, I in) {
        IStrategyFactory factory = factoryMap.get(scope.getClass());
        if (factory == null) {
            log.error("[process] no factory. scope={}, in={}", scope, in);
            throw new RuntimeException("un support publisher");
        }
        return (O) factory.process(scope, in);
    }
}

3、使用示例

IStrategyDispatcher dispatcher = new DefaultStrategyDispatcher();
dispatcher.register(strategy...);

Object test = dispatcher.process(ScopeTest.T1, "test");
System.out.println(test);

初始化IStrategyDispatcher之后,把所有加了@StrategyScope注解的策略注册进去,触发策略的时候直接调用process即可。

相关文章

网友评论

      本文标题:策略模式

      本文链接:https://www.haomeiwen.com/subject/eopsudtx.html