策略模式

定义

策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。

使用场景

  • 针对同一类型问题的多种处理方式,仅仅是具体行为有差别时。
  • 需要安全地封装多种同一类型的操作时。
  • 出现同一抽象类有多个子类,而又需要时if-else或者switch-case类选择具体子类时。

uml图

策略模式uml
  • Context:用来操作策略的上下文环境。
  • Starategy:策略的抽象。
  • Concreate StrategyA、Concreate StrategyB:具体策略实现。

具体代码

以为对价格进行处理为例。对价格进行取整,策略一:四舍五入。策略二:抹去所有小数。

//策略的接口
interface PriceStrategy {
    int calPrice(double price);
}
//具体的不同策略

//四舍五入
class RoundPrice implements PriceStrategy {

    @Override
    public int calPrice(double price) {
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负");
        }
        if (price == 0d) {
            return 0;
        } else {
            return (int) (price + 0.5);
        }
    }
}

//取整
class ArrangementPrice implements PriceStrategy {

    @Override
    public int calPrice(double price) {
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负");
        }
        return (int) price;
    }
}

首先,确定统一的调用规范,通过calPrice方法获取最后的数据,再创建具体的策略实现类:RoundPriceArrangementPrice

//策略的使用场景
class PriceCal {
    private PriceStrategy strategy = new RoundPrice();

    private double realPrice;

    public PriceCal(double realPrice) {
        this.realPrice = realPrice;
    }

    public PriceStrategy getStrategy() {
        return strategy;
    }

    public void setStrategy(PriceStrategy strategy) {
        this.strategy = strategy;
    }

    public int getPaidPrice() {
        return strategy.calPrice(realPrice);
    }
}

再实现策略的使用场景类。通过设置strategy属性来使用不同的策略。
使用方式

    public static void main(String[] args) {
        PriceCal priceCal = new PriceCal(10.72);
        //设置需要的策略
        priceCal.setStrategy(new RoundPrice());
        System.out.println(priceCal.getPaidPrice());
    }

上面可以看出,策略模式在使用的优势。具体的策略实现已经和策略的使用场景相分离。当需要添加新的策略时,只需要实现接口,再在使用时添加就可以。而if-else的方法这需要修改使用场景类,来解决这个问题。

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