作用:
提供一个接口,返回子对象
将bean注入工厂方式:
1.@Component +构造器:
AbstractLogicChain 实现ILogicChain接口
@Component("rule_blacklist") public class BlackListLogicChain extends AbstractLogicChain
@Component("default") public class DefaultLogicChain extends AbstractLogicChain
@Component("rule_weight") public class RuleWeightLogicChain extends AbstractLogicChain
private final Map<String, ILogicChain> logicChainGroup;
protected IStrategyRepository repository;
public DefaultChainFactory(Map<String, ILogicChain> logicChainGroup, IStrategyRepository repository) { this.logicChainGroup = logicChainGroup; this.repository = repository; }
2.@Resource +map:
//伪代码
config类:
@Resource
BlackListLogicChain blackListLogicChain ;
@Resource
RuleWeightLogicChain ruleWeightLogicChain ;
map.put("rule_blacklist", blackListLogicChain)
factory工厂继承config
3.注解: 策略模式? 策略工厂!
自定义注解
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface LogicStrategy { DefaultLogicFactory.LogicModel logicMode(); }
每个子类加注解
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_LOCK) public class RuleLockLogicFilter implements ILogicFilter<RuleActionEntity.RaffleCenterEntity> {
@Service public class DefaultLogicFactory { //装配 public Map<String, ILogicFilter<?>> logicFilterMap = new ConcurrentHashMap<>(); public DefaultLogicFactory(List<ILogicFilter<?>> logicFilters) { logicFilters.forEach(logic -> { LogicStrategy strategy = AnnotationUtils.findAnnotation(logic.getClass(), LogicStrategy.class); if (null != strategy) { logicFilterMap.put(strategy.logicMode().getCode(), logic); } }); } public <T extends RuleActionEntity.RaffleEntity> Map<String, ILogicFilter<T>> openLogicFilter() { return (Map<String, ILogicFilter<T>>) (Map<?, ?>) logicFilterMap; } @Getter @AllArgsConstructor public enum LogicModel { RULE_WIGHT("rule_weight", "【抽奖前规则】根据抽奖权重返回可抽奖范围KEY", "before"), RULE_BLACKLIST("rule_blacklist", "【抽奖前规则】黑名单规则过滤,命中黑名单则直接返回", "before"), RULE_LOCK("rule_lock", "【抽奖中规则】抽奖n次后,对应奖品可解锁抽奖", "center"), RULE_LUCK_AWARD("rule_luck_award", "【抽奖后规则】抽奖n次后,对应奖品可解锁抽奖", "after"), ; private final String code; private final String info; private final String type; }
和第一种方式差不多
标签:Map,抽奖,class,rule,工厂,RULE,模式,设计模式,public From: https://blog.csdn.net/m0_75137040/article/details/137398200