设计模式-策略模式

定义

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

分析

策略模式要求算法封装以后能相互替换,就是要求封装以后的对象有共性,很自然的,我们想到所有的算法实现相同的接口,通过接口来约束算法的操作。

类图

Client StrategyFactory List<Strategy> Strategys Strategy getStrategy «interface» Strategy void execute ConcreteStrategy1 concreteStrategy1(Reciever1 reciever) void execute ConcreteStrategy2 concreteStrategy2(Reciever2 reciever) void execute ConcreteStrategy3 concreteStrategy3(Reciever3 reciever) void execute

抽象策略(Strategy)类

定义了一个公共接口,各种不同的算法以不同的方式实现这个接口,客户使用这个接口调用不同的算法,一般使用接口或抽象类实现。

具体策略(Concrete Strategy)类

实现了抽象策略定义的接口,提供具体的算法实现。

策略工厂(StrategyFactory)类

封装选择具体策略的逻辑,为客户提供具体策略。

代码

这里举一个商场打折的例子。不同等级的会员打折方式不同。

抽象策略接口

public interface CalPrice {
    //根据原价返回一个最终的价格
    Double calPrice(Double originalPrice);
}

具体策略

class Common implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice;
    }

}
class Vip implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.8;
    }

}
class SuperVip implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.7;
    }

}
class GoldVip implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.5;
    }
}

策略工厂

public class CalPriceFactory {

    private CalPriceFactory(){}
    //根据客户的总金额产生相应的策略
    public static CalPrice createCalPrice(Customer customer){
        if (customer.getTotalAmount() > 3000) {//3000则改为金牌会员计算方式
            return new GoldVip();
        }else if (customer.getTotalAmount() > 2000) {//类似
            return new SuperVip();
        }else if (customer.getTotalAmount() > 1000) {//类似
            return new Vip();
        }else {
            return new Common();
        }
    }
}

客户类

public class Customer {

    private Double totalAmount = 0D;//客户在本商店消费的总额
    private Double amount = 0D;//客户单次消费金额
    private CalPrice calPrice = new Common();//每个客户都有一个计算价格的策略,初始都是普通计算,即原价
    
    //客户购买商品,就会增加它的总额
    public void buy(Double amount){
        this.amount = amount;
        totalAmount += amount;
        /* 变化点,我们将策略的制定转移给了策略工厂,将这部分责任分离出去 */
        calPrice = CalPriceFactory.createCalPrice(this);
    }
    //计算客户最终要付的钱
    public Double calLastAmount(){
        return calPrice.calPrice(amount);
    }
    
    public Double getTotalAmount() {
        return totalAmount;
    }
    
    public Double getAmount() {
        return amount;
    }
}

提升

基本的代码就这些了,但是在策略工厂中还是有大量的if else存在,代码不是优雅,如果需要新增策略,还需要在策略工厂中添加选择策略的判断方法。策略模式的一个主要目的是减少if else代码的难以维护的问题。
针对上面举的这个例子,由于策略工厂中是主要针对totalAmount来进行判断的,这样可以在策略类中添加判断策略适用的条件,在策略工厂初始化的时候,将策略类动态注册进去。选择策略类的时候,直接使用totalAmount调用判断策略适用的条件,如果条件满足,就选中该方法。

抽象策略接口

public interface CalPrice {
    //根据原价返回一个最终的价格
    Double calPrice(Double originalPrice);
    boolean isSuitable(Double totalAmount);
}

具体策略

class Common implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice;
    }
    boolean isSuitable(Double totalAmount){
       if (totalAmount <= 1000)
       return true;
       return false;
    }

}
class Vip implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.8;
    }
    boolean isSuitable(Double totalAmount){
       if (totalAmount > 1000 && totalAmount <= 2000)
       return true;
       return false;
    }

}
class SuperVip implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.7;
    }
    boolean isSuitable(Double totalAmount){
       if (totalAmount > 2000 && totalAmount <= 3000)
       return true;
       return false;
    }

}
class GoldVip implements CalPrice{

    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.5;
    }
    boolean isSuitable(Double totalAmount){
       if (totalAmount > 3000)
       return true;
       return false;
    }
}

策略工厂

public class CalPriceFactory {
    private static List<CalPrice> CalPrices = new ArraryList();

    public CalPriceFactory(){
    
       CalPrices.add(GoldVip());
       CalPrices.add(SuperVip());
       CalPrices.add(Vip());
       CalPrices.add(Common());
    
    }
    //根据客户的总金额产生相应的策略
    public static CalPrice createCalPrice(Customer customer){
        for (CalPrice calPrice : CalPrices ){
             if (calPrice.isSuitable(customer.getTotalAmount()))
                return calPrice;
        }
        return NULL;
    }
}

后记

当然,这里判断策略是否适合的逻辑也可以使用注解来实现,在策略工厂中对注解进行处理即可。这里面还可以增加对于多个策略同时共同生效的处理,可以在每个策略上添加优先级,配置是否允许同时生效等等处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值