利用策略模式改善代码中的if嵌套

      什么是策略模式?其思想是针对一组算法,将每一种算法都封装到具有共同接口的独立的类中,从而是它们可以相互替换。策略模式的最大特点是使得算法可以在不影响客户端的情况下发生变化,从而改变不同的功能。

     假如我们有一个根据不同用户类型返回不同折扣的方法,我们的实现可能是这样:

import org.springframework.stereotype.Service;

@Service
public class CashService {

    public double cash(String type, double money) {
        if ("svip".equals(type)) {
            return money * 0.75;
        } else if ("vip".equals(type)) {
            return money * 0.9;
        } else {
            return money;
        }
    }
}

    现在我们各个类型的用户折扣耦合在一起,修改一个用户的折扣力度有可能会对其他类型用户造成影响。根据策略模式的思想,我们需要把折扣力度封装成具体的方法并面向接口编程。我们首先定义公共的接口DiscountService,编写其实现类,则我们改造后的代码可能如下所示:

     

import com.study.designer.strategy.NormalDiscountStrategy;
import com.study.designer.strategy.SvipDiscountStrategy;
import com.study.designer.strategy.VipDiscountStrategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CashService {


    @Autowired
    private SvipDiscountStrategy svipDiscountStrategy;

    @Autowired
    private VipDiscountStrategy vipDiscountStrategy;

    @Autowired
    private NormalDiscountStrategy normalDiscountStrategy;

    public double cash(String type, double money) {
        if ("svip".equals(type)) {
            return svipDiscountStrategy.getMoney(money);
        } else if ("vip".equals(type)) {
            return vipDiscountStrategy.getMoney(money);
        } else {
            return normalDiscountStrategy.getMoney(money);
        }
    }
}

      可以看到,改造后的CashService中还存在许多if判断,我们需要消除这些if判断。我们可以在CashService初始化时就获取到所有的折扣策略,然后根据具体类型计算具体折扣。获取所有策略可以交由Spring来完成,改造后的代码如下所示:

import com.study.designer.strategy.inf.DiscountStrategy;

import org.springframework.stereotype.Service;

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

@Service
public class CashService {


    private Map<String, DiscountStrategy> strategyMap = new HashMap<>();


    public CashService(List<DiscountStrategy> strategies) {
        for (DiscountStrategy strategy : strategies) {
            strategyMap.put(strategy.getType(), strategy);
        }
    }


    public double cash(String type, double money) {
        return strategyMap.get(type).getMoney(money);
    }
}

     

猜你喜欢

转载自www.cnblogs.com/wkzhao/p/10229412.html