package com.miaxis.common.strategy;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 收藏列表策略管理
 * @author wwl
 * @date 2021/7/26 15:29
 */
@Slf4j
public class StrategyManager {

    private static StrategyManager strategyManager = new StrategyManager();
    private Map<String, Strategy> strategyMap = new HashMap<>();
    private Map<String, Strategy> defaultStrategyMap = new HashMap<>();

    private StrategyManager() {
    }

    public static StrategyManager getInstance() {
        return strategyManager;
    }

    public void register(Strategy strategy) {
        this.register(strategy, false);
    }

    //策略注册
    public void register(Strategy strategy, boolean isDefault) {
        String strategyName = strategy.getName();
        strategyMap.put(strategyName, strategy);
        if (isDefault) {
            defaultStrategyMap.put(strategyName, strategy);
        }
    }

    public void unload(Strategy strategy) {
        String strategyName = strategy.getName();
        Strategy defaultStrategy = defaultStrategyMap.get(strategyName);
        if (defaultStrategy != null) {
            strategyMap.put(strategyName, defaultStrategy);
        } else {
            strategyMap.remove(strategyName);
        }
    }

    /**
     * 获取策略
     * @param strategyName
     * @return
     */
    public Strategy getStrategy(String strategyName) {
        return strategyMap.get(strategyName);
    }
}