注解实现策略模式

代码结构如下

注解实现策略模式_第1张图片

代码如下

@Getter
@AllArgsConstructor
public enum AuditPlutusEnum {

    BACK("回款"),

    REFUND("退款"),

    INVOICE("开票");

    private final String name;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface AuditStrategyType {

    @AliasFor(annotation = Component.class)
    String value() default "";

    AuditPlutusEnum type();


}
 */
@Configuration
public class AuditStrategyConfig {

    @Bean
    public AuditStrategyRunner auditStrategyRunner(List<AuditPlutusStrategy> strategies) {
        Map<AuditPlutusEnum, AuditPlutusStrategy> strategyMap = strategies.stream()
                .collect(Collectors.toMap(
                        // 获取策略标识
                        s -> s.getClass().getAnnotation(AuditStrategyType.class).type(),
                        s -> s
                ));
        return (flag, auditPlutusRo) -> strategyMap.get(flag).audit(auditPlutusRo);
    }
}
public interface AuditStrategyRunner {


    void audit(AuditPlutusEnum type, AuditPlutusRo auditPlutusRo);
}
public interface AuditPlutusStrategy {


    void audit(AuditPlutusRo ro);
}
@AuditStrategyType(type = AuditPlutusEnum.REFUND)
public class AuditRefundStrategy implements AuditPlutusStrategy {

    @Override
    public void audit(AuditPlutusRo ro) {
        // todo: 2024/1/24  退款相关处理
    }
}```

```java
@AuditStrategyType(type = AuditPlutusEnum.INVOICE)
public class AuditInvoiceStrategy implements AuditPlutusStrategy {

    @Override
    public void audit(AuditPlutusRo ro) {
        // todo: 2024/1/24  开票相关处理
    }
}
@AuditStrategyType(type = AuditPlutusEnum.BACK)
public class AuditBackStrategy implements AuditPlutusStrategy {

    @Override
    public void audit(AuditPlutusRo ro) {
        // todo: 2024/1/24  回款相关处理
    }
}

调用


    @Autowired
    private AuditStrategyRunner auditStrategyRunner;

    public ResultData<?> auditBack(AuditPlutusRo ro) {
        log.info("【{}】审核-业财回调处理,审核编号为:{},传入参数为:{}", ro.getAuditPlutusEnum().getName(), ro.getAuditNo(), JSON.toJSONString(ro));
        auditStrategyRunner.audit(ro.getAuditPlutusEnum(), ro);
        return ResultData.SUCCESS;
    }

你可能感兴趣的:(策略模式,python,windows)