策略模式和工厂模式共舞——支付路由设计
站长
· 阅读数 20
策略模式是一种行为型设计模式,它定义了一系列算法,并将每个算法封装到一个类中,使得它们可以相互替换。这样可以让算法的变化独立于使用算法的客户端代码。策略模式的核心思想是将算法的实现和使用分离开来,从而提高代码的灵活性和可维护性。
工厂模式则是一种创建型设计模式,它提供了一种方式来创建对象,而不需要直接调用构造函数或者其他创建对象的代码。工厂模式隐藏了对象的创建过程,使得客户端代码只关注于如何使用对象,而不需要知道对象的具体创建过程。工厂模式的核心思想是将对象的创建和使用分离开来,从而提高代码的复用性和可扩展性。
现在我们来看一个案例,假设我们正在开发一个电商网站,其中有一个订单类,它有不同的支付方式。我们希望能够让用户在下单时选择不同的支付方式,而且还要支持添加新的支付方式。
首先,我们定义一个支付策略接口:
package com.example.demo.strategy;
public interface PaymentStrategy {
/**
* 获取支付方式
* @return
*/
Integer getPayType();
/**
* 进行支付
* @param amount
*/
void pay(double amount);
}
然后,我们创建几个支付策略类,这里以支付宝支付和微信支付为例:
支付方式枚举类:
package com.example.demo.strategy;
public enum PayTypeEnum {
ALI_PAY(1, "支付宝"),
WECHAT_PAY(2, "微信支付");
private final Integer code;
private final String desc;
PayTypeEnum(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public Integer getCode() {
return code;
}
}
支付宝实现类:
package com.example.demo.strategy;
import org.springframework.stereotype.Component;
@Component("aliPay")
public class AliPayStrategy implements PaymentStrategy {
@Override
public Integer getPayType() {
return PayTypeEnum.ALI_PAY.getCode();
}
@Override
public void pay(double amount) {
// 支付宝支付逻辑
System.out.println("使用支付宝支付:" + amount + "元");
}
}
微信支付实现类:
package com.example.demo.strategy;
import org.springframework.stereotype.Component;
@Component("weChatPay")
public class WeChatPayStrategy implements PaymentStrategy {
@Override
public Integer getPayType() {
return PayTypeEnum.WECHAT_PAY.getCode();
}
@Override
public void pay(double amount) {
// 微信支付逻辑
System.out.println("使用微信支付:" + amount + "元");
}
}
接下来,我们创建一个工厂类来创建支付策略对象:
package com.example.demo.strategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class PaymentStrategyFactory implements InitializingBean {
/**
* 通过注解直接将所有PaymentStrategy组件注入到list中
*/
@Autowired
private List<PaymentStrategy> paymentStrategies;
Map<Integer, PaymentStrategy> paymentStrategyMap = new HashMap<>();
/**
* 构建payType为key的paymentStrategyMap
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
for (PaymentStrategy paymentStrategy : paymentStrategies) {
paymentStrategyMap.put(paymentStrategy.getPayType(), paymentStrategy);
}
}
/**
* 获取具体的实现类
* @param payType
* @return
*/
public PaymentStrategy getPaymentStrategy(Integer payType) {
return paymentStrategyMap.get(payType);
}
}
最后,我们在订单服务类,将支付策略对象的创建过程交给工厂类完成:
package com.example.demo.strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class OrderService {
@Autowired
private PaymentStrategyFactory paymentStrategyFactory;
public void payOrder(Integer payType, double amount) {
/**
* 获取支付渠道
*/
PaymentStrategy paymentStrategy = paymentStrategyFactory.getPaymentStrategy(payType);
paymentStrategy.pay(amount);
}
}
bean配置类:
package com.example.demo.strategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.demo.strategy")
public class AppConfig {
@Bean("aliPay")
public PaymentStrategy aliPayStrategy() {
return new AliPayStrategy();
}
@Bean("weChatPay")
public PaymentStrategy weChatPayStrategy() {
return new WeChatPayStrategy();
}
}
Main类:
package com.example.demo.strategy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
OrderService orderService = context.getBean(OrderService.class);
orderService.payOrder(PayTypeEnum.ALI_PAY.getCode(), 135.1);
orderService.payOrder(PayTypeEnum.WECHAT_PAY.getCode(), 112.3);
}
}
运行效果图: 策略模式+工厂模式的运用,既简单又实用,是生产实践经常使用的模式,我们要对它有深刻的认识,并运用自如。
欢迎关注公众号:程序员的思考与落地
公众号提供大量实践案例,Java入门者不容错过哦,可以交流!!