当前位置: 首页 > news >正文

Springboot之策略模式

Springboot之策略模式

  • 策略模式的几种方式
    • 1 简单实现
      • 1.1 创建策略接口
      • 1.2 实现付款方式
        • 1.2.1 微信付款
        • 1.2.2 支付宝付款
      • 1.3 创建策略调度器
      • 1.4 创建配置类

策略模式的几种方式

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 {@Overridepublic String getType() {return "weChatPay";}@Overridepublic 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 {@Overridepublic String getType() {return "aliPay";}@Overridepublic 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*/@Beanpublic 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 {@Autowiredprivate PayStrategyHandler handler;/*** 用户付款** @return*/@RequestMapping(value = "strategy", method = RequestMethod.GET)public String pay() {String type = "weChatPay";handler.run(type);return "付款成功";}
}
http://www.lryc.cn/news/284587.html

相关文章:

  • HTTP 协议和 TCP/IP 协议之间有什么区别?
  • 【Effective C++】让自己习惯C++
  • 第十一章 请求响应
  • 【React】脚手架创建项目
  • 力扣70. 爬楼梯(动态规划 Java,C++解法)
  • Wpf 使用 Prism 实战开发Day13
  • 62 C++ 多线程 -- mutex互斥量只能使用一次的问题分析-----以及解决方案递归mutex:recursive_mutex。
  • Chrome Devtools 调试指南
  • 【Qt5】QString的成员函数chop
  • Spring中的注解
  • JavaScript 中的事件
  • hasattr、getattr、setattr
  • 构建高可用消息队列系统 01
  • 十本你不容错过的Docker入门到精通书籍推荐
  • 【AI接口】语音版、文心一言大模型和AI绘图、图片检测API
  • VUE 中的 v-for 和 v-if 是否可以共存
  • kubernetes 权限控制
  • vue中父组件异步传值,渲染问题
  • 09前后端分离+SSM整合的小案例
  • 模仿ProTable创建ProTable组件
  • 新品发布 | 多通道总线记录仪TLog1004,是你期待的吗?
  • Double数据类型保留3位小数
  • 08- OpenCV:形态学操作(膨胀与腐蚀 、提取水平与垂直线)
  • 基于JavaWeb+SSM+Vue停车场微信小程序系统的设计和实现
  • VUE---自定义指令
  • 开发安全之:Cross-Site Scripting (XSS) 漏洞
  • 代码随想录算法训练营第二十四天| 77. 组合
  • 虚拟歌姬学习:DiffSinger,让GitHub下载快的方法!
  • What is `StringEscapeUtils.escapeHtml4` does?
  • Dubbo 的心脏:理解和应用多种协议【十三】