JAVA设计模式-行为型模式-策略模式DEMO

场景:写订单支付场景的代码时,客户可以选择多种支付方式,有银联支付、支付宝支付、微信支付等等。

1-正常编写if判断支付类型,如下伪代码

  /**
     * 订单支付方法
     *
     * @return
     */
    public boolean pay(String payType) {
        // 是否支付成功
        boolean payment = false;
        if ("aliPay".equals(payType)) {
            System.out.println("支付宝支付");
                    payment = true;
        } else if ("unionPay".equals(payType)) {
            System.out.println("银联支付");
                    payment = true;
        } else if ("wechatPay".equals(payType)) {
            System.out.println("微信支付");
                    payment = true;
        }

        return payment;
    }

 2- 策略模式编写

1-编写支付接口

/**
 * <简述>创建抽象的支付接口 PayType ,让不同的支付逻辑类都实现此接口
 * <详细描述>
 *
 * @author syf
 * @date 2023年07月21日 10:04
 */
public interface  PayType {

    /**
     * <简述>
     * <详细描述>
     * @author syf
     * @date 2023/7/21 10:05
     * @param orderId 订单id
     * @param amount 支付金额
     * @return boolean
     */
    boolean pay(String orderId, Long amount);
}

2-编写接口的实现类(支付宝 微信 等等等)

支付宝支付接口

/**
 * <简述>支付宝支付
 * <详细描述>
 * @author syf
 * @date 2023年07月21日 10:05
 */
public class AliPay implements PayType {

    @Override
    public boolean pay(String orderId, Long amount) {
        System.out.println("支付宝支付");
        return false;
    }
}

3-微信支付接口

/**
 * <简述>微信支付
 * <详细描述>
 * @author syf
 * @date 2023年07月21日 10:05
 */
public class WeChatPay implements PayType {

    @Override
    public boolean pay(String orderId, Long amount) {
        System.out.println("微信支付");
        return false;
    }
}

4-订单类


@Data
//y
@AllArgsConstructor
public class Order {
    // 订单id
    private String orderId;
    // 金额
    private long amount;

    // 支付类型
    private PayType payType;


    /**
     * 订单支付方法
     */
    public boolean pay() {
        boolean isSuccess;
        // 调用支付接口
        isSuccess = payType.pay(orderId, amount);

        if (!isSuccess) {
            // 支付失败逻辑
            System.out.println("支付失败!");
        }
        return isSuccess;
    }
}

5- 测试结果

测试类编写调用支付

@RunWith(SpringRunner.class)
public class Test {


    @org.junit.Test
    public void test() {
        // 创建支付策略
        PayType aliPay = new AliPay();

        // 创建订单
        String orderId = "6666";
        long amount = 6666;
        Order order = new Order(orderId, amount, aliPay );

        //调用支付接口
        order.pay();
    }
}

10:27:38.377 [main] DEBUG org.springframework.test.context.cache - Spring test ApplicationContext cache statistics: [DefaultContextCache@5b12b668 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 4, missCount = 1]
支付宝支付
支付失败!

 依赖

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值