模拟支付分为 微信支付 ,支付宝支付,银联支付
1、工厂模式
(1)、支付枚举类
import lombok.Getter;/*** @author zwh* @date 2024/1/26*/
@Getter
public enum PayEnum {/*** 支付类型*/WX_PAY(1, "微信支付"),ALI_PAY(2, "支付宝支付"),UNION_PAY(3, "银联支付"),;private final Integer code;private final String message;PayEnum(Integer code, String message) {this.code = code;this.message = message;}/*** 检查支付编码** @param payCode 支付编码* @return boolean*/public static boolean checkPayCode(Integer payCode) {for (PayEnum payEnum : PayEnum.values()) {if (payEnum.getCode().equals(payCode)) {return true;}}return false;}
}
(2)、支付接口
如果需要额外设置属性,需要使用抽象类,但是这里的支付仅仅只用到支付方法,所以不需要使用抽象类
/*** @author zwh* @date 2024/1/26*/
public interface PayService {/*** 支付*/void goToPay();
}
(3)、支付方法
import org.springframework.stereotype.Service;/*** @author zwh* @date 2024/1/26*/
@Service
public class AliPayServiceImpl implements PayService {@Overridepublic void goToPay() {System.out.println("zfb");}
}
import org.springframework.stereotype.Service;/*** @author zwh* @date 2024/1/26*/
@Service
public class WxPayServiceImpl implements PayService {@Overridepublic void goToPay() {System.out.println("wx");}
}
import org.springframework.stereotype.Service;/*** @author zwh* @date 2024/1/26*/
@Service
public class UnionPayServiceImpl implements PayService {@Overridepublic void goToPay() {System.out.println("yl");}
}
(4)、支付工厂创建
import com.pay.payservice.PayEnum;
import org.springframework.stereotype.Component;/*** 支付工厂** @author zwh* @date 2024/1/26*/
@Component
public class PayFactory {public PayService getInstance(PayEnum payEnum) {PayService payService;switch (payEnum) {case WX_PAY -> {payService = new WxPayServiceImpl();break;}case ALI_PAY -> {payService = new AliPayServiceImpl();break;}case UNION_PAY -> {payService = new UnionPayServiceImpl();break;}default -> throw new RuntimeException("支付类型错误,支付失败!");}return payService;}
}
(5)、测试
@SpringBootTest
class PayServiceApplicationTests {@Testvoid contextLoads() {}@Testvoid payTest() {PayFactory payFactory = new PayFactory();System.out.println("简单工厂模式");PayService wxPayService = payFactory.getInstance(PayEnum.WX_PAY);wxPayService.goToPay();PayService aliPayService = payFactory.getInstance(PayEnum.ALI_PAY);aliPayService.goToPay();PayService unionPayService = payFactory.getInstance(PayEnum.UNION_PAY);unionPayService.goToPay();}
}
打印结果
简单工厂模式
wx
zfb
yl
2、策略模式和工厂模式组合
目的: 使用策略模式去掉冗余的if-else 或者switch
通用上面的PayEnum
(1)、支付工厂类
import com.pay.payservice.PayEnum;import java.util.HashMap;
import java.util.Map;/*** 工厂策略模式** @author zwh* @date 2024/1/26*/
public class PayFactoryStrategy {/*** 设置策略参数*/private static final Map<Integer, PayStrategyService> STRATEGY_MAP = new HashMap<>();public static PayStrategyService getInstance(Integer payCode) {if (STRATEGY_MAP.containsKey(payCode)) {return STRATEGY_MAP.get(payCode);}throw new RuntimeException("没有找到对应的支付策略,获取支付方法失败");}public static void register(Integer payCode, PayStrategyService payService) {boolean isContain = PayEnum.checkPayCode(payCode);if (isContain) {STRATEGY_MAP.put(payCode, payService);} else {throw new RuntimeException("没有找到对应的支付策略,注入失败");}}
}
(2)、支付接口
import org.springframework.beans.factory.InitializingBean;/*** @author zwh* @date 2024/1/26*/
public interface PayStrategyService extends InitializingBean {/*** 支付*/void goToPay();
}
这里继承了 InitializingBean 类的主要目的是在启动的时候把对象给注册进去
(3)、支付接口实现类
import com.pay.payservice.PayEnum;
import org.springframework.stereotype.Service;/*** @author zwh* @date 2024/1/26*/
@Service
public class WxPayStrategyServiceImpl implements PayStrategyService {@Overridepublic void goToPay() {System.out.println("策略工厂-微信支付");}@Overridepublic void afterPropertiesSet() throws Exception {PayFactoryStrategy.register(PayEnum.WX_PAY.getCode(), this);}
}
import com.pay.payservice.PayEnum;
import org.springframework.stereotype.Service;/*** @author zwh* @date 2024/1/26*/
@Service
public class AliPayStrategyServiceImpl implements PayStrategyService {@Overridepublic void goToPay() {System.out.println("工厂策略模式-支付宝支付");}@Overridepublic void afterPropertiesSet() throws Exception {PayFactoryStrategy.register(PayEnum.ALI_PAY.getCode(), this);}
}
import com.pay.payservice.PayEnum;
import org.springframework.stereotype.Service;/*** @author zwh* @date 2024/1/26*/
@Service
public class UnionPayStrategyServiceImpl implements PayStrategyService {@Overridepublic void goToPay() {System.out.println("策略工厂-银联支付");}@Overridepublic void afterPropertiesSet() throws Exception {PayFactoryStrategy.register(PayEnum.UNION_PAY.getCode(), this);}
}
(4)、测试
@SpringBootTest
class PayServiceApplicationTests {@Testvoid payStrategyTest() {System.out.println("策略工厂模式");PayStrategyService wxStrategyPay = PayFactoryStrategy.getInstance(PayEnum.WX_PAY.getCode());wxStrategyPay.goToPay();PayStrategyService aliStrategyPay = PayFactoryStrategy.getInstance(PayEnum.ALI_PAY.getCode());aliStrategyPay.goToPay();PayStrategyService unionStrategyPay = PayFactoryStrategy.getInstance(PayEnum.UNION_PAY.getCode());unionStrategyPay.goToPay();}}
返回结果
策略工厂模式
策略工厂-微信支付
工厂策略模式-支付宝支付
策略工厂-银联支付