计算机网站开发专业站长工具域名查询ip
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 "付款成功";}
}