spring策略模式实战

package com.ab.dh.datahouse.service.scheduled.xxljob.salary.service.impl.monthkpi;

import com.ab.dh.datahouse.service.scheduled.xxljob.salary.service.KpiProcessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author DJ031300 月度kpi策略实现类
 */
@Service
public class MonthKpiStrategyImpl {
    private final List serviceList = new CopyOnWriteArrayList<>();

    @Autowired
    public MonthKpiStrategyImpl(List strategyList) {
        this.serviceList.clear();
        strategyList.forEach(item -> this.serviceList.add(item));
    }

    public KpiProcessService getStrategyImpl(String cL2DptCode) {
        if (StringUtils.isBlank(cL2DptCode)) {
            return null;
        }

        for (KpiProcessService service : serviceList) {
            if (service.supportProcessSecondOrg(cL2DptCode)) {
                return service;
            }
        }

        return null;
    }
}

利用 @Autowired 自动获取接口的实现类并封装到 list 中

你可能感兴趣的:(状态模式)