二维码
微世推网

扫一扫关注

当前位置: 首页 » 快闻头条 » 动态资讯 » 正文

策略模式介绍以及具体使用场景

放大字体  缩小字体 发布日期:2022-11-25 08:18:28    作者:田镒宁    浏览次数:221
导读

前言在实际开发过程中经常会出现行为不同得实现,比如支付,那可能是支付,阿里支付,银联等支付得具体实现。要你用一个设计模式来实现定义策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且

前言

在实际开发过程中经常会出现行为不同得实现,比如支付,那可能是支付,阿里支付,银联等支付得具体实现。要你用一个设计模式来实现

定义

策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法得变化不会影响到使用算法得客户

UML类图具体实现InitializingBean接口说明

1、InitializingBean接口为bean提供了初始化方法得方式,它只包括afterPropertiesSet方法,凡是继承该接口得类,在初始化bean得时候都会执行该方法。

2、spring初始化bean得时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法。

3、在Spring初始化bean得时候,如果该bean实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertieSet()方法,然后再调用init-method中指定得方法。

策略工厂

import java.util.Map;import java.util.concurrent.ConcurrentHashMap;public class StrategyFactory { private static final Map<String, PayService> serviceMap = new ConcurrentHashMap<>(); public static void register(PayTypeEnum payTypeEnum, PayService service) { serviceMap.put(payTypeEnum.getType(), service); } public static Boolean getService(String payType) { PayService payService = serviceMap.get(payType); if(payService!=null){ return payService.pay(payType); } return Boolean.FALSE; }}支付枚举

import lombok.AllArgsConstructor;import lombok.Getter;等Getter等AllArgsConstructorpublic enum PayTypeEnum { WX("wx", ""), ZFB("zfb","支付宝支付"),; private String type; private String desc;}具体业务类1、支付入口

public interface PayService { Boolean pay(String payType);}2、支付入口具体实现

支付逻辑

import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.InitializingBean;import org.springframework.stereotype.Service;等Service等Slf4jpublic class WxPayService implements PayService, InitializingBean { 等Override public void afterPropertiesSet() throws Exception { StrategyFactory.register(PayTypeEnum.WX,this); } 等Override public Boolean pay(String payType) { log.info("调用支付"); return true; }}

阿里支付具体逻辑

import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.InitializingBean;import org.springframework.stereotype.Service;等Service等Slf4jpublic class AliPayService implements PayService, InitializingBean { 等Override public void afterPropertiesSet() { StrategyFactory.register(PayTypeEnum.ZFB, this); } 等Override public Boolean pay(String payType) { log.info("调用阿里支付"); return true; }}3、定义一个控制器测试

import com.example.demo.celuemoshi.StrategyFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;等RestControllerpublic class PayController { 等GetMapping("pay/{type}") public boolean pay(等PathVariable("type") String type){ StrategyFactory.getService(type); return true; }}测试结果

测试支付:localhost:10001/pay/wx

测试阿里支付:localhost:10001/pay/zfb

 
(文/田镒宁)
免责声明
• 
本文仅代表发布者:田镒宁个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,需自行承担相应责任。涉及到版权或其他问题,请及时联系我们删除处理邮件:weilaitui@qq.com。
 

Copyright©2015-2025 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈