策略模式减少代码的if else判断

策略模式(Strategy Pattern)

在策略模式中,一个类的行为或其算法可以在运行时更改,这种类型的设计模式属于行为型设计模式

在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的context对象,策略对象改变context对象的执行算法

介绍

意图:定义一系列的算法,把它们封装起来,并且使他们可以相互替换

主要解决:在有多种算法相似的情况下,使用if else带来的复杂和难以维护

如何使用:一个系统有许多类,区分他们的只是他们直接的行为

如何解决:将这些算法封装成一个个的类,任意调换

关键代码:实现同一个接口

实际应用实例:比如消息的推送(微信公众号,短信,钉钉...),支付方式的不同,结算不同(微信支付,支付宝支付,银行卡支付...)Java AWT中的LayOutManger

优点:算法自由切换,减少if else复杂逻辑,扩展性良好

缺点:策略类会增多,所有的策略类都需要对外暴露

如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。

demo

1:枚举类,形如你有哪些支付方式,哪些推送消息的方式

public enum ReChargeEnum {
    E_Bink(1,"网银"),

    MOBILE(3,"手机充值"),

    PAYPLAY(4,"支付宝"),

    WEIXING(5,"微信"),

    BUSI_ACCOUNT(2,"商户");

    private int value;
    private String descreption;

    ReChargeEnum(int value, String descreption) {
        this.value = value;
        this.descreption = descreption;
    }

    public static ReChargeEnum valueOf(int value){
        for (ReChargeEnum reChargeEnum:
             ReChargeEnum.values()) {
            if (reChargeEnum.value==value){
                return reChargeEnum;
            }
        }
        return null;
    }
get set...

2:策略的接口,比如是付款,那他们统一的工作都是付款,只是支付方式不一样,即为实现了不一样

/**
* 支付
*/
public interface StrategyService {
     Double calReCharge(Double charge,ReChargeEnum reChargeEnum);
}

3:具体的支付方式(此处就不一一列举)实现支付接口

public class WeiXinPayServiceImpl implements StrategyService {
    @Override
    public Double calReCharge(Double charge, ReChargeEnum reChargeEnum) {
        return charge*0.85;
    }
}

4:一个生产这些支付方式的工厂类

public class StrategyFactory {
    private static StrategyFactory strategyFactory=new StrategyFactory();

    public StrategyFactory() {
    }

    public static Map<Integer,StrategyService> strategyServiceMap=new HashMap<>();

    static {
        strategyServiceMap.put(ReChargeEnum.WEIXING.getValue(),new WeiXinPayServiceImpl());
        strategyServiceMap.put(ReChargeEnum.E_Bink.getValue(),new BUSIPayServiceImpl());
        ...
        ...
    }

    public static StrategyService create(Integer type){
        return strategyServiceMap.get(type);
    }
     public static StrategyFactory getStrategyFactory(){
        return strategyFactory;
     }
}

5:一个对外提供算法的简单类Context

public class Context {
    private StrategyService strategyService;
    public double calCharge(double charge,Integer type){
        strategyService=  StrategyFactory.getStrategyFactory().create(type);
        return strategyService.calReCharge(charge,ReChargeEnum.valueOf(type));
    }

    public StrategyService getStrategyService() {
        return strategyService;
    }

    public void setStrategyService(StrategyService strategyService) {
        this.strategyService = strategyService;
    }
}

6:使用案例

  public static void main(String[] args) {
        Context context=new Context();
        double calCharge = context.calCharge(100d, 5);
        System.out.println("微信支付:"+calCharge);
        double calCharge1 = context.calCharge(100d, 1);
        System.out.println("网银支付:"+calCharge1);
    }

不用再像这样

public class Example {  
        public Double calRecharge(Double charge ,RechargeTypeEnum type ){     
            if(type.equals(RechargeTypeEnum.E_BANK)){  
                return charge*0.85;  
            }else if(type.equals(RechargeTypeEnum.BUSI_ACCOUNTS)){  
                return charge*0.90;  
            }else if(type.equals(RechargeTypeEnum.MOBILE)){  
                return charge;  
            }else if(type.equals(RechargeTypeEnum.CARD_RECHARGE)){  
                return charge+charge*0.01;  
            }else{  
                return null;  
            }  
        }  
    }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用策略模式的案例: 假设我们有一个订单处理系统,订单可以使用不同的支付方式进行支付,包括微信、支付宝和银行卡。在原始实现中,我们需要使用if else语句来判断不同的支付方式。这样会导致代码的可读性和可维护性降低,因为每次新增或修改支付方式都需要修改if else代码段。 现在,我们使用策略模式来重构这个系统。首先,我们定义一个支付接口`PaymentStrategy`,并在接口中定义了一个`pay()`方法: ```java public interface PaymentStrategy { public void pay(double amount); } ``` 然后,我们实现不同的支付方式,包括微信支付、支付宝支付和银行卡支付: ```java public class WechatPayment implements PaymentStrategy { public void pay(double amount) { System.out.println("使用微信支付 " + amount + " 元"); } } public class AlipayPayment implements PaymentStrategy { public void pay(double amount) { System.out.println("使用支付宝支付 " + amount + " 元"); } } public class CreditCardPayment implements PaymentStrategy { public void pay(double amount) { System.out.println("使用信用卡支付 " + amount + " 元"); } } ``` 接下来,我们定义一个订单处理类`OrderService`,并在类中注入支付策略`PaymentStrategy`: ```java public class OrderService { private PaymentStrategy paymentStrategy; public OrderService(PaymentStrategy paymentStrategy) { this.paymentStrategy = paymentStrategy; } public void processOrder(double amount) { paymentStrategy.pay(amount); } } ``` 最后,我们在Spring配置文件中定义不同的支付策略,然后使用`@Autowired`注解来注入不同的支付策略: ```xml <bean id="wechatPayment" class="com.example.payment.WechatPayment" /> <bean id="alipayPayment" class="com.example.payment.AlipayPayment" /> <bean id="creditCardPayment" class="com.example.payment.CreditCardPayment" /> <bean id="orderService" class="com.example.order.OrderService"> <constructor-arg> <ref bean="wechatPayment" /> </constructor-arg> </bean> ``` 现在,我们可以通过修改Spring配置文件来切换不同的支付方式,而不需要修改业务逻辑代码。这样,我们就使用策略模式减少了if else语句的使用,提高了代码的可读性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值