自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

策略模式介紹以及具體使用場景

開發(fā) 前端
InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候都會執(zhí)行該方法。

前言

在實(shí)際開發(fā)過程中經(jīng)常會出現(xiàn)行為不同的實(shí)現(xiàn),比如支付,那可能是微信支付,阿里支付,銀聯(lián)等支付的具體實(shí)現(xiàn)。要你用一個設(shè)計(jì)模式來實(shí)現(xiàn)

定義

策略模式定義了一系列算法,并將每個算法封裝起來,使他們可以相互替換,且算法的變化不會影響到使用算法的客戶

UML類圖

具體實(shí)現(xiàn)

InitializingBean接口說明

1、InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候都會執(zhí)行該方法。

2、spring初始化bean的時候,如果bean實(shí)現(xiàn)了InitializingBean接口,會自動調(diào)用afterPropertiesSet方法。

3、在Spring初始化bean的時候,如果該bean實(shí)現(xiàn)了InitializingBean接口,并且同時在配置文件中指定了init-method,系統(tǒng)則是先調(diào)用afterPropertieSet()方法,然后再調(diào)用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
@AllArgsConstructor
public enum PayTypeEnum {
WX("wx", "微信"),
ZFB("zfb","支付寶支付"),;
private String type;
private String desc;
}

具體業(yè)務(wù)類

1、支付入口

public interface PayService {
Boolean pay(String payType);
}

2、支付入口具體實(shí)現(xiàn)

微信支付邏輯

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WxPayService implements PayService, InitializingBean {

@Override
public void afterPropertiesSet() throws Exception {
StrategyFactory.register(PayTypeEnum.WX,this);
}

@Override
public Boolean pay(String payType){
log.info("調(diào)用微信支付");
return true;
}
}

阿里支付具體邏輯

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;


import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AliPayService implements PayService, InitializingBean {
@Override
public void afterPropertiesSet(){
StrategyFactory.register(PayTypeEnum.ZFB, this);
}
@Override
public Boolean pay(String payType){
log.info("調(diào)用阿里支付");
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;

@RestController
public class PayController {

@GetMapping("pay/{type}")
public boolean pay(@PathVariable("type") String type){
StrategyFactory.getService(type);
return true;
}
}

測試結(jié)果

測試微信支付:http://localhost:10001/pay/wx

測試阿里支付:http://localhost:10001/pay/zfb

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2020-04-07 14:20:10

RabbitMMySQL數(shù)據(jù)庫

2023-03-20 09:17:13

策略模式Springboot

2023-02-22 09:16:22

2021-08-29 22:05:04

對象自動回收

2023-06-06 08:18:24

Kafka架構(gòu)應(yīng)用場景

2015-06-26 11:33:23

Python裝飾器使用場景實(shí)踐

2021-11-04 06:58:32

策略模式面試

2020-10-29 07:16:26

布隆過濾器場景

2014-05-15 09:43:11

CloudaMobile WebANodejs

2023-05-16 07:47:18

RabbitMQ消息隊(duì)列系統(tǒng)

2018-08-15 09:48:27

數(shù)據(jù)庫Redis應(yīng)用場景

2021-04-27 08:31:10

前端應(yīng)用場景

2021-08-06 10:43:56

Kubernetes容器

2020-06-16 15:40:32

閉鎖柵欄線程

2009-09-24 16:55:27

策略模式

2023-10-29 15:21:42

負(fù)載均衡器分布式系統(tǒng)后端

2022-10-12 07:24:18

大文件哈希算法Hash

2024-05-11 12:47:16

Kafka場景.高性能

2013-12-25 16:03:39

GitGit 命令

2022-05-13 07:26:28

策略模式設(shè)計(jì)模式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號