java strategy模式,Java设计模式——策略模式(Strategy pattern)

策略模式,在阿里巴巴开发手册中有提到过,可以用来减少if-else嵌套,是一个常用的设计模式。策略模式结构如下图,一个上下文Context只有一个抽象的策略Strategy,可以调用该策略的strategyInterface()执行具体的策略方法。就比如平常用手机支付,我的手机持有系统级别的抽象的支付方式,然后支付宝和微信支付来具体实现我的支付接口,用户需要付款时,弹窗让用户选择使用哪种方式付款,选择完成后就会调用具体的app去付款了。

7928d14d8f29

Strategy pattern

废话不多说,上代码如下:

首先,我有一个抽象的系统级别的支付策略PayStrategy接口。

package me.zebin.designpattern.strategy;

// 抽像的系统级别的接口

public interface PayStrategy {

void pay();

}

这个接口在我的手机StrateMain系统里边

package me.zebin.designpattern.strategy;

// 我的手机里有一个抽象的支付接口

public class StrategyMain {

private static PayStrategy payStrategy;

}

微信、支付宝都能够支付,都来实现我的系统级别的支付接口。

微信支付:

package me.zebin.designpattern.strategy;

public class WxPayStrategyImpl implements PayStrategy {

public void pay() {

System.out.println("正在使用微信支付");

}

}

支付宝支付:

package me.zebin.designpattern.strategy;

public class AliPayStrategyImpl implements PayStrategy {

public void pay() {

System.out.println("正在使用支付宝支付");

}

}

然后让用户选择一种支付方式,我们用这种支付方式付款。具体的支付策略都实现在具体的类中了,我们只要持有抽象对象就可执行具体的方法。

package me.zebin.designpattern.strategy;

public class StrategyMain {

private static PayStrategy payStrategy;

public static void main(String[] args) {

// 用户选择的支付方式

String payment = "alipay";

if(payment.equals("wxpay")){

payStrategy = new WxPayStrategyImpl();

}else if(payment.equals("alipay")){

payStrategy = new AliPayStrategyImpl();

}

payStrategy.pay();

}

}

然而,上面的方法并不是最佳实践,因为StrateMain.main()中最终还是需要做一堆的if-else判断,个人认为最好的设计如下

策略模式 + 简单工厂模式(不在23中设计模式之中)+ spring IOC才是最优雅的设计。实现一个简单工厂模式,使用spring IOC将具体的策略类注入到Map中。

新建一个简单工厂,使用@Autowired将所有PayStrategy注入到Map中。

package me.zebin.designpattern.strategy;

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

import org.springframework.stereotype.Component;

import java.util.Map;

@Component

public class PayStrategyFactory {

// 借助spring容器,所有PayStrategy的实现类都会注入到map中,key为bean的名字

@Autowired

private Map payStrategyMap;

public PayStrategy getStrategy(String key){

return payStrategyMap.get(key);

}

}

因为如上注入的map中,key为bean名,所以微信、支付宝的@Component需要重新命名,如下

package me.zebin.designpattern.strategy;

import org.springframework.stereotype.Component;

@Component("alipay")

public class AliPayStrategyImpl implements PayStrategy {

public void pay() {

System.out.println("正在使用支付宝支付");

}

}

微信支付重新命名:

package me.zebin.designpattern.strategy;

import org.springframework.stereotype.Component;

@Component("wxpay")

public class WxPayStrategyImpl implements PayStrategy {

public void pay() {

System.out.println("正在使用微信支付");

}

}

这样,通过Spring IOC + 简单工厂模式,就不需要加入if-else的判断了

package me.zebin.designpattern.strategy.test;

import me.zebin.designpattern.strategy.PayStrategy;

import me.zebin.designpattern.strategy.PayStrategyFactory;

import org.junit.Test;

import org.junit.runner.RunWith;

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

import org.springframework.boot.SpringBootConfiguration;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest

public class StrategyIOCMainTest {

@Autowired

private PayStrategyFactory payStrategyFactory;

@Test

public void execPay() {

// 用户选择的支付方式

String payment = "alipay";

// 直接传入支付的名称获取bean

PayStrategy p = payStrategyFactory.getStrategy(payment);

p.pay();

}

}

支付成功,如图:

7928d14d8f29

支付成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值