策略模式+工厂模式+模板模式混合使用

现实中存在这种业务场景,通过交易号执行不同的业务代码逻辑,就算把业务方法封装成不同的方法也会出现很多的if-else,代码看起来很臃肿。如果需要添加新的交易还需要添加新的il-else。

String transNo = "30012400";
if (transNo == "30012400") {
	new TransNo30012400().function();
} else if (transNo == "30012401") {
	new TransNo30012401().function();
} else if (transNo == "30012402") {
	new TransNo30012402().function();
} else if (transNo == "30012403") {
	new TransNo30012403().function();
} else if (transNo == "30012404") {
	new TransNo30012404().function();
} else if (transNo == "30012405") {
	new TransNo30012405().function();
} else if (transNo == "30012406") {
	new TransNo30012406().function();
}

新建AbstractHandler类,编写所有可能执行方法的抽象方法

public abstract class AbstractHandler implements InitializingBean {

	/**
	 * 交易处理方法-返回类型为String
	 * 
	 * @param name
	 */
	public String processingTypeString(String transNo) {
		throw new UnsupportedOperationException();
	}

	/**
	 * 交易处理方法-返回类型为BigDecimal
	 * 
	 * @param name
	 */
	public BigDecimal processingTypeBigDecimal(String transNo) {
		throw new UnsupportedOperationException();
	}
}

新建交易工厂,用于存储以后的交易类

public class TransFactory {

	private static Map<String, AbstractHandler> transMap = Maps.newHashMap();

	public static AbstractHandler getInvokHandler(String transNo) {
		return transMap.get(transNo);
	}

	public static void register(String transNo, AbstractHandler handler) {
		if (StringUtils.isEmpty(transNo) || null == handler) {
			return;
		}
		transMap.put(transNo, handler);
	}
}

新建两个交易类,必须添加@Component,交个Spring管理,AbstractHandler实现了InitializingBean接口,这个方法afterPropertiesSet会在类加载的时候执行

@Component
public class TransNo30012405 extends AbstractHandler {

	@Override
	public void afterPropertiesSet() throws Exception {
		TransFactory.register("30012405", this);
	}

	public String processingTypeString(String transNo) {
		// TODO业务逻辑
		return "交易成功";
	}
}

@Component
public class TransNo30012406 extends AbstractHandler {

	@Override
	public void afterPropertiesSet() throws Exception {
		TransFactory.register("30012406", this);
	}

	public BigDecimal processingTypeBigDecimal(String transNo) {
		// TODO业务逻辑
		return BigDecimal.ONE;
	}
}

测试用例

@Test
public void functionDemo() {
	String transNo = "30012405";
	AbstractHandler invokHandler = TransFactory.getInvokHandler(transNo);
	String str = invokHandler.processingTypeString(transNo);
	System.out.println(str);
	transNo = "30012406";
	AbstractHandler invokHandler1 = TransFactory.getInvokHandler(transNo);
	BigDecimal bigDecimal = invokHandler1.processingTypeBigDecimal(transNo);
	System.out.println(bigDecimal);
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的问题。下面是我对这个问题的回答: 首先,我们需要了解一下策略模式工厂模式模板方法模式的概念和用法: - 策略模式:将一组算法封装起来,使它们可以互相替换,从而使得算法的变化不会影响到使用算法的客户端。在策略模式中,我们需要定义一个策略接口和具体的策略实现类。 - 工厂模式:将对象的创建与使用分离,通过工厂类来创建对象,从而使得客户端无需知道对象的具体实现。在工厂模式中,我们需要定义一个工厂接口和具体的工厂实现类。 - 模板方法模式:将一组相似的算法步骤封装起来,使得算法的变化只会影响到模板方法的具体实现,从而使得代码的复用性和可维护性更好。 在实现不同支付方式的接口时,我们可以使用策略模式来实现不同的支付策略,工厂模式来创建不同的支付策略对象,模板方法模式来实现支付的公共流程。 具体实现步骤如下: 1. 定义支付策略接口 PaymentStrategy,其中包含一个 pay 方法用来实现支付功能。 ```java public interface PaymentStrategy { void pay(double amount); } ``` 2. 定义具体的支付策略实现类,例如 AlipayPaymentStrategy 和 WechatPaymentStrategy,实现 PaymentStrategy 接口中的 pay 方法。 ```java public class AlipayPaymentStrategy implements PaymentStrategy { @Override public void pay(double amount) { // 实现支付宝支付逻辑 System.out.println("使用支付宝支付:" + amount + "元"); } } public class WechatPaymentStrategy implements PaymentStrategy { @Override public void pay(double amount) { // 实现微信支付逻辑 System.out.println("使用微信支付:" + amount + "元"); } } ``` 3. 定义工厂接口 PaymentFactory,其中包含一个 createPayment 方法用来创建支付策略对象。 ```java public interface PaymentFactory { PaymentStrategy createPayment(); } ``` 4. 定义具体的支付工厂实现类,例如 AlipayPaymentFactory 和 WechatPaymentFactory,实现 PaymentFactory 接口中的 createPayment 方法。 ```java public class AlipayPaymentFactory implements PaymentFactory { @Override public PaymentStrategy createPayment() { return new AlipayPaymentStrategy(); } } public class WechatPaymentFactory implements PaymentFactory { @Override public PaymentStrategy createPayment() { return new WechatPaymentStrategy(); } } ``` 5. 定义支付模板类 PaymentTemplate,其中包含一个 pay 方法用来实现支付的公共流程,该方法调用 PaymentFactory 创建支付策略对象,并调用支付策略的 pay 方法实现支付功能。 ```java public abstract class PaymentTemplate { public void pay(double amount) { PaymentStrategy paymentStrategy = createPaymentStrategy(); paymentStrategy.pay(amount); } protected abstract PaymentStrategy createPaymentStrategy(); } ``` 6. 定义具体的支付模板类实现类,例如 AlipayPaymentTemplate 和 WechatPaymentTemplate,实现 PaymentTemplate 中的 createPaymentStrategy 方法,返回对应的支付策略对象。 ```java public class AlipayPaymentTemplate extends PaymentTemplate { @Override protected PaymentStrategy createPaymentStrategy() { return new AlipayPaymentFactory().createPayment(); } } public class WechatPaymentTemplate extends PaymentTemplate { @Override protected PaymentStrategy createPaymentStrategy() { return new WechatPaymentFactory().createPayment(); } } ``` 7. 在 SpringBoot 项目中使用支付模板类实现不同支付方式的接口,例如 PaymentController,其中包含一个 pay 方法用来接收支付请求,根据支付方式调用对应的支付模板类实现支付功能。 ```java @RestController public class PaymentController { @GetMapping("/pay") public String pay(@RequestParam("amount") double amount, @RequestParam("type") String type) { PaymentTemplate paymentTemplate = null; if ("alipay".equals(type)) { paymentTemplate = new AlipayPaymentTemplate(); } else if ("wechat".equals(type)) { paymentTemplate = new WechatPaymentTemplate(); } else { return "不支持的支付方式:" + type; } paymentTemplate.pay(amount); return "支付成功:" + amount + "元"; } } ``` 这样,我们就可以通过策略模式工厂模式模板方法模式来实现不同支付方式的接口,使得代码更加灵活、可维护性更好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值