Springboot之策略模式

策略模式的几种方式

1 简单实现

场景:策略模式实现不同类型的付款动作

1.1 创建策略接口

package com.per.strategy;

/**
 * @Title Strategy
 * @Description TODO
 * @Author Lee
 * @Date 2024-01-20
 */
public interface PayStrategy {
   

    /**
     * 付款方式
     *
     * @return
     */
    String getType();

    /**
     * 执行策略
     */
    void process();

}

1.2 实现付款方式

1.2.1 微信付款
package com.per.strategy.service;

import com.per.strategy.PayStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Title WeChatPayService
 * @Description TODO
 * @Author Lee
 * @Date 2024-01-20
 */
@Component
@Slf4j
public class WeChatPayService implements PayStrategy {
   

    @Override
    public String getType() {
   
        return "weChatPay";
    }

    @Override
    public void process() {
   
        log.info("微信付款100元");
    }
}
1.2.2 支付宝付款
package com.per.strategy.service;

import com.per.strategy.PayStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Title AliPayService
 * @Description TODO
 * @Author Lee
 * @Date 2024-01-20
 */
@Component
@Slf4j
public class AliPayService implements PayStrategy {
   
    
    @Override
    public String getType() {
   
        return "aliPay";
    }

    @Override
    public void process() {
   
        log.info("支付宝付款100元");
    }
}

1.3 创建策略调度器

package com.per.strategy;

/**
 * @Title PayStrategyHandler
 * @Description TODO
 * @Author Lee
 * @Date 2024-01-20
 */
public interface PayStrategyHandler {
   

    /**
     * 执行策略
     *
     * @param type 付款方式
     */
    void run(String type);
}

1.4 创建配置类

package com.per.strategy.config;

import com.per.strategy.PayStrategy;
import com.per.strategy.PayStrategyHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title PayStrategyConfig
 * @Description TODO
 * @Author Lee
 * @Date 2024-01-20
 */
@Configuration
public class PayStrategyConfig {
   

    /**
     * 注册策略调度器
     *
     * @param payStrategies
     * @return
     */
    @Bean
    public PayStrategyHandler handler(List<PayStrategy> payStrategies) {
   
        Map<String, PayStrategy> strategyMaps = payStrategies.stream().collect(Collectors.toMap(PayStrategy::getType, item -> item));
//        return new PayStrategyHandler() {
   
//            @Override
//            public void run(String type) {
   
//                strategyMaps.get(type).process();
//            }
//        };
        return type -> strategyMaps.get(type).process();
    }
}

实际使用如下:

package com.per.controller;

import com.per.strategy.PayStrategyHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title UserController
 * @ProjectName spring-boot-demo
 * @Description TODO
 * @Author Lee
 * @Date 2024-01-17
 */
@RestController
public class UserController {
   

    @Autowired
    private PayStrategyHandler handler;

    /**
     * 用户付款
     *
     * @return
     */
    @RequestMapping(value = "strategy", method = RequestMethod.GET)
    public String pay() {
   
        String type = "weChatPay";
        handler.run(type);
        return "付款成功";
    }
}

相关推荐

  1. Springboot策略模式

    2024-01-21 01:16:02       37 阅读
  2. 设计模式策略模式

    2024-01-21 01:16:02       30 阅读
  3. 设计模式策略模式

    2024-01-21 01:16:02       33 阅读
  4. 设计模式策略模式

    2024-01-21 01:16:02       18 阅读
  5. 设计模式策略模式

    2024-01-21 01:16:02       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-21 01:16:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-21 01:16:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-21 01:16:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-21 01:16:02       20 阅读

热门阅读

  1. 第四讲_ArkTS装饰器(一)

    2024-01-21 01:16:02       39 阅读
  2. WebSocket

    WebSocket

    2024-01-21 01:16:02      28 阅读
  3. 铺设道路——贪心

    2024-01-21 01:16:02       36 阅读
  4. 算法训练营Day37(贪心6)

    2024-01-21 01:16:02       44 阅读