策略模式+工厂模式
public interface PaymentService {
void processPayment(BigDecimal amount);
}
@Service("ALIPAY")
public class AlipayService implements PaymentService {
@Override
public void processPayment(BigDecimal amount) {
System.out.println("支付宝支付: " + amount);
}
}
@Service("WECHAT")
public class WechatPayService implements PaymentService {
@Override
public void processPayment(BigDecimal amount) {
System.out.println("微信支付: " + amount);
}
}
@Component
public class PaymentServiceFactory {
private final Map<String, PaymentService> paymentServices;
// 自动注入所有 PaymentService 实现类
@Autowired
public PaymentServiceFactory(Map<String, PaymentService> paymentServices) {
this.paymentServices = paymentServices;
}
// 根据 type 获取对应的 Service
public PaymentService getService(String type) {
PaymentService service = paymentServices.get(type);
if (service == null) {
throw new IllegalArgumentException("无效支付类型: " + type);
}
return service;
}
}
@RestController
public class PaymentController {
@Autowired
private PaymentServiceFactory paymentServiceFactory;
@PostMapping("/pay")
public String pay(@RequestParam String type, @RequestParam BigDecimal amount) {
PaymentService service = paymentServiceFactory.getService(type);
service.processPayment(amount);
return "支付成功";
}
}
——————————————————————————————————————————————————
自定义注解 + 动态映射
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PaymentType {
String value();
}
@Service
@PaymentType("ALIPAY")
public class AlipayService implements PaymentService {
@Override
public void processPayment(BigDecimal amount) {
System.out.println("支付宝支付: " + amount);
}
}
@Service
@PaymentType("WECHAT")
public class WechatPayService implements PaymentService {
@Override
public void processPayment(BigDecimal amount) {
System.out.println("微信支付: " + amount);
}
}
@Component
public class PaymentServiceFactory {
private final Map<String, PaymentService> paymentServices = new HashMap<>();
// 初始化时扫描所有带有 @PaymentType 注解的 Bean
@Autowired
public void initPaymentServices(List<PaymentService> services) {
for (PaymentService service : services) {
PaymentType annotation = service.getClass().getAnnotation(PaymentType.class);
if (annotation != null) {
paymentServices.put(annotation.value(), service);
}
}
}
public PaymentService getService(String type) {
PaymentService service = paymentServices.get(type);
if (service == null) {
throw new IllegalArgumentException("无效支付类型: " + type);
}
return service;
}
}
}
@RestController
public class PaymentController {
@Autowired
private PaymentServiceFactory paymentServiceFactory; // 注入工厂类:ml-citation{ref="4" data="citationList"}
@PostMapping("/pay")
public ResponseEntity<?> processPayment(@RequestParam String type, @RequestParam BigDecimal amount) {
// 根据 type 获取具体 Service 实现类
PaymentService service = paymentServiceFactory.getService(type);
service.processPayment(amount);
return ResponseEntity.ok("支付成功");
}
}
策略模式怎么写
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 别在满屏幕写if-else了。使用策略模式。真香 先看原始伪代码 看下使用了策略模式,判断逻辑。 是不是很清爽呢?...
- 来自公众号:程序员小灰作者:LA 我们使用的app大多都有分享的功能,我们可以选择分享到不同的地方,比如微博、微信...
- 前言:策略模式和适配器模式很像 但前者策略的接口和相关类会暴露出来,并且每个策略的“计算内容”都不同【常用于计...
- 来源公众号:程序员小灰作者:LA 我们使用的app大多都有分享的功能,我们可以选择分享到不同的地方,比如微博、微信...