国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

一起看看責任鏈設計模式吧!

Android編程精選 ? 來源:CSDN ? 作者:Java小海 ? 2022-07-08 16:25 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

  • 什么是責任鏈
  • 場景
    • 反例
    • 初步改造
    • 缺點
    • 責任鏈改造
    • 責任鏈工廠改造
  • 聊聊其他

最近,我讓團隊內一位成員寫了一個導入功能。他使用了責任鏈模式,代碼堆的非常多,bug 也多,沒有達到我預期的效果。實際上,針對導入功能,我認為模版方法更合適!為此,隔壁團隊也拿出我們的案例,進行了集體 code review。

學好設計模式,且不要為了練習,強行使用!讓原本100行就能實現的功能,寫了 3000 行!

對錯暫且不論,我們先一起看看責任鏈設計模式吧!

什么是責任鏈

「責任鏈模式」 是一種行為設計模式, 允許你將請求沿著處理者鏈進行發送。收到請求后, 每個處理者均可對請求進行處理, 或將其傳遞給鏈上的下個處理者。

3dca85f4-fc56-11ec-ba43-dac502259ad0.png

基于 Spring Boot + MyBatis Plus + Vue & Element 實現的后臺管理系統 + 用戶小程序,支持 RBAC 動態權限、多租戶、數據權限、工作流、三方登錄、支付、短信、商城等功能。

項目地址:https://github.com/YunaiV/ruoyi-vue-pro

場景

責任鏈的使用場景還是比較多的

  • 多條件流程判斷:權限控制
  • ERP 系統流程審批:總經理、人事經理、項目經理
  • Java 過濾器 的底層實現 Filter

如果不使用該設計模式,那么當需求有所改變時,就會使得代碼臃腫或者難以維護,例如下面的例子

反例

假設現在有一個闖關游戲,進入下一關的條件是上一關的分數要高于xx

  1. 游戲一共 3 個關卡
  2. 進入第二關需要第一關的游戲得分大于等于 80
  3. 進入第三關需要第二關的游戲得分大于等于 90

那么代碼可以這樣寫

//第一關
publicclassFirstPassHandler{
publicinthandler(){
System.out.println("第一關-->FirstPassHandler");
return80;
}
}

//第二關
publicclassSecondPassHandler{
publicinthandler(){
System.out.println("第二關-->SecondPassHandler");
return90;
}
}


//第三關
publicclassThirdPassHandler{
publicinthandler(){
System.out.println("第三關-->ThirdPassHandler,這是最后一關啦");
return95;
}
}

//客戶端
publicclassHandlerClient{
publicstaticvoidmain(String[]args){

FirstPassHandlerfirstPassHandler=newFirstPassHandler();//第一關
SecondPassHandlersecondPassHandler=newSecondPassHandler();//第二關
ThirdPassHandlerthirdPassHandler=newThirdPassHandler();//第三關

intfirstScore=firstPassHandler.handler();
//第一關的分數大于等于80則進入第二關
if(firstScore>=80){
intsecondScore=secondPassHandler.handler();
//第二關的分數大于等于90則進入第二關
if(secondScore>=90){
thirdPassHandler.handler();
}
}
}
}

那么如果這個游戲有100關,我們的代碼很可能就會寫成這個樣子

if(第1關通過){
//第2關游戲
if(第2關通過){
//第3關游戲
if(第3關通過){
//第4關游戲
if(第4關通過){
//第5關游戲
if(第5關通過){
//第6關游戲
if(第6關通過){
//...
}
}
}
}
}
}

這種代碼不僅冗余,并且當我們要將某兩關進行調整時會對代碼非常大的改動,這種操作的風險是很高的,因此,該寫法非常糟糕

初步改造

如何解決這個問題,我們可以通過鏈表將每一關連接起來,形成責任鏈的方式,第一關通過后是第二關,第二關通過后是第三關 ....,這樣客戶端就不需要進行多重 if 的判斷了

publicclassFirstPassHandler{
/**
*第一關的下一關是第二關
*/
privateSecondPassHandlersecondPassHandler;

publicvoidsetSecondPassHandler(SecondPassHandlersecondPassHandler){
this.secondPassHandler=secondPassHandler;
}

//本關卡游戲得分
privateintplay(){
return80;
}

publicinthandler(){
System.out.println("第一關-->FirstPassHandler");
if(play()>=80){
//分數>=80并且存在下一關才進入下一關
if(this.secondPassHandler!=null){
returnthis.secondPassHandler.handler();
}
}

return80;
}
}

publicclassSecondPassHandler{

/**
*第二關的下一關是第三關
*/
privateThirdPassHandlerthirdPassHandler;

publicvoidsetThirdPassHandler(ThirdPassHandlerthirdPassHandler){
this.thirdPassHandler=thirdPassHandler;
}

//本關卡游戲得分
privateintplay(){
return90;
}

publicinthandler(){
System.out.println("第二關-->SecondPassHandler");

if(play()>=90){
//分數>=90并且存在下一關才進入下一關
if(this.thirdPassHandler!=null){
returnthis.thirdPassHandler.handler();
}
}

return90;
}
}

publicclassThirdPassHandler{

//本關卡游戲得分
privateintplay(){
return95;
}

/**
*這是最后一關,因此沒有下一關
*/
publicinthandler(){
System.out.println("第三關-->ThirdPassHandler,這是最后一關啦");
returnplay();
}
}

publicclassHandlerClient{
publicstaticvoidmain(String[]args){

FirstPassHandlerfirstPassHandler=newFirstPassHandler();//第一關
SecondPassHandlersecondPassHandler=newSecondPassHandler();//第二關
ThirdPassHandlerthirdPassHandler=newThirdPassHandler();//第三關

firstPassHandler.setSecondPassHandler(secondPassHandler);//第一關的下一關是第二關
secondPassHandler.setThirdPassHandler(thirdPassHandler);//第二關的下一關是第三關

//說明:因為第三關是最后一關,因此沒有下一關
//開始調用第一關每一個關卡是否進入下一關卡在每個關卡中判斷
firstPassHandler.handler();

}
}

缺點

現有模式的缺點

  • 每個關卡中都有下一關的成員變量并且是不一樣的,形成鏈很不方便
  • 代碼的擴展性非常不好

責任鏈改造

  • 既然每個關卡中都有下一關的成員變量并且是不一樣的,那么我們可以在關卡上抽象出一個父類或者接口,然后每個具體的關卡去繼承或者實現

有了思路,我們先來簡單介紹一下責任鏈設計模式的「基本組成」

  • 抽象處理者(Handler)角色:定義一個處理請求的接口,包含抽象處理方法和一個后繼連接。
  • 具體處理者(Concrete Handler)角色:實現抽象處理者的處理方法,判斷能否處理本次請求,如果可以處理請求則處理,否則將該請求轉給它的后繼者。
  • 客戶類(Client)角色:創建處理鏈,并向鏈頭的具體處理者對象提交請求,它不關心處理細節和請求的傳遞過程。
3de1a3a6-fc56-11ec-ba43-dac502259ad0.jpg責任鏈改造
publicabstractclassAbstractHandler{

/**
*下一關用當前抽象類來接收
*/
protectedAbstractHandlernext;

publicvoidsetNext(AbstractHandlernext){
this.next=next;
}

publicabstractinthandler();
}

publicclassFirstPassHandlerextendsAbstractHandler{

privateintplay(){
return80;
}

@Override
publicinthandler(){
System.out.println("第一關-->FirstPassHandler");
intscore=play();
if(score>=80){
//分數>=80并且存在下一關才進入下一關
if(this.next!=null){
returnthis.next.handler();
}
}
returnscore;
}
}

publicclassSecondPassHandlerextendsAbstractHandler{

privateintplay(){
return90;
}

publicinthandler(){
System.out.println("第二關-->SecondPassHandler");

intscore=play();
if(score>=90){
//分數>=90并且存在下一關才進入下一關
if(this.next!=null){
returnthis.next.handler();
}
}

returnscore;
}
}

publicclassThirdPassHandlerextendsAbstractHandler{

privateintplay(){
return95;
}

publicinthandler(){
System.out.println("第三關-->ThirdPassHandler");
intscore=play();
if(score>=95){
//分數>=95并且存在下一關才進入下一關
if(this.next!=null){
returnthis.next.handler();
}
}
returnscore;
}
}

publicclassHandlerClient{
publicstaticvoidmain(String[]args){

FirstPassHandlerfirstPassHandler=newFirstPassHandler();//第一關
SecondPassHandlersecondPassHandler=newSecondPassHandler();//第二關
ThirdPassHandlerthirdPassHandler=newThirdPassHandler();//第三關

//和上面沒有更改的客戶端代碼相比,只有這里的set方法發生變化,其他都是一樣的
firstPassHandler.setNext(secondPassHandler);//第一關的下一關是第二關
secondPassHandler.setNext(thirdPassHandler);//第二關的下一關是第三關

//說明:因為第三關是最后一關,因此沒有下一關

//從第一個關卡開始
firstPassHandler.handler();

}
}

責任鏈工廠改造

對于上面的請求鏈,我們也可以把這個關系維護到配置文件中或者一個枚舉中。我將使用枚舉來教會大家怎么動態的配置請求鏈并且將每個請求者形成一條調用鏈。

publicenumGatewayEnum{
//handlerId,攔截者名稱,全限定類名,preHandlerId,nextHandlerId
API_HANDLER(newGatewayEntity(1,"api接口限流","cn.dgut.design.chain_of_responsibility.GateWay.impl.ApiLimitGatewayHandler",null,2)),
BLACKLIST_HANDLER(newGatewayEntity(2,"黑名單攔截","cn.dgut.design.chain_of_responsibility.GateWay.impl.BlacklistGatewayHandler",1,3)),
SESSION_HANDLER(newGatewayEntity(3,"用戶會話攔截","cn.dgut.design.chain_of_responsibility.GateWay.impl.SessionGatewayHandler",2,null)),
;

GatewayEntitygatewayEntity;

publicGatewayEntitygetGatewayEntity(){
returngatewayEntity;
}

GatewayEnum(GatewayEntitygatewayEntity){
this.gatewayEntity=gatewayEntity;
}
}

publicclassGatewayEntity{

privateStringname;

privateStringconference;

privateIntegerhandlerId;

privateIntegerpreHandlerId;

privateIntegernextHandlerId;
}

publicinterfaceGatewayDao{

/**
*根據handlerId獲取配置項
*@paramhandlerId
*@return
*/
GatewayEntitygetGatewayEntity(IntegerhandlerId);

/**
*獲取第一個處理者
*@return
*/
GatewayEntitygetFirstGatewayEntity();
}

publicclassGatewayImplimplementsGatewayDao{

/**
*初始化,將枚舉中配置的handler初始化到map中,方便獲取
*/
privatestaticMapgatewayEntityMap=newHashMap<>();

static{
GatewayEnum[]values=GatewayEnum.values();
for(GatewayEnumvalue:values){
GatewayEntitygatewayEntity=value.getGatewayEntity();
gatewayEntityMap.put(gatewayEntity.getHandlerId(),gatewayEntity);
}
}

@Override
publicGatewayEntitygetGatewayEntity(IntegerhandlerId){
returngatewayEntityMap.get(handlerId);
}

@Override
publicGatewayEntitygetFirstGatewayEntity(){
for(Map.Entryentry:gatewayEntityMap.entrySet()){
GatewayEntityvalue=entry.getValue();
//沒有上一個handler的就是第一個
if(value.getPreHandlerId()==null){
returnvalue;
}
}
returnnull;
}
}

publicclassGatewayHandlerEnumFactory{

privatestaticGatewayDaogatewayDao=newGatewayImpl();

//提供靜態方法,獲取第一個handler
publicstaticGatewayHandlergetFirstGatewayHandler(){

GatewayEntityfirstGatewayEntity=gatewayDao.getFirstGatewayEntity();
GatewayHandlerfirstGatewayHandler=newGatewayHandler(firstGatewayEntity);
if(firstGatewayHandler==null){
returnnull;
}

GatewayEntitytempGatewayEntity=firstGatewayEntity;
IntegernextHandlerId=null;
GatewayHandlertempGatewayHandler=firstGatewayHandler;
//迭代遍歷所有handler,以及將它們鏈接起來
while((nextHandlerId=tempGatewayEntity.getNextHandlerId())!=null){
GatewayEntitygatewayEntity=gatewayDao.getGatewayEntity(nextHandlerId);
GatewayHandlergatewayHandler=newGatewayHandler(gatewayEntity);
tempGatewayHandler.setNext(gatewayHandler);
tempGatewayHandler=gatewayHandler;
tempGatewayEntity=gatewayEntity;
}
//返回第一個handler
returnfirstGatewayHandler;
}

/**
*反射實體化具體的處理者
*@paramfirstGatewayEntity
*@return
*/
privatestaticGatewayHandlernewGatewayHandler(GatewayEntityfirstGatewayEntity){
//獲取全限定類名
StringclassName=firstGatewayEntity.getConference();
try{
//根據全限定類名,加載并初始化該類,即會初始化該類的靜態段
Classclazz=Class.forName(className);
return(GatewayHandler)clazz.newInstance();
}catch(ClassNotFoundException|IllegalAccessException|InstantiationExceptione){
e.printStackTrace();
}
returnnull;
}
}

publicclassGetewayClient{
publicstaticvoidmain(String[]args){
GetewayHandlerfirstGetewayHandler=GetewayHandlerEnumFactory.getFirstGetewayHandler();
firstGetewayHandler.service();
}
}

基于微服務的思想,構建在 B2C 電商場景下的項目實戰。核心技術棧,是 Spring Boot + Dubbo 。未來,會重構成 Spring Cloud Alibaba 。

項目地址:https://github.com/YunaiV/onemall

聊聊其他

設計模式有很多,責任鏈只是其中的一種,我覺得很有意思,非常值得一學。

設計模式確實是一門藝術,仍需努力呀

審核編輯 :李倩


聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • JAVA
    +關注

    關注

    20

    文章

    3001

    瀏覽量

    116422
  • 代碼
    +關注

    關注

    30

    文章

    4968

    瀏覽量

    73960
  • 過濾器
    +關注

    關注

    1

    文章

    444

    瀏覽量

    20964

原文標題:同事寫了一個責任鏈模式,bug無數!

文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    為什么不能將 USB 磁盤與 exFat 一起使用?

    不能將 USB 磁盤與 exFat 一起使用
    發表于 03-04 06:27

    阿里狗16.6,層疊表能打開,但是打開后內容縮在一起,調整間距之后,關閉在打開還是縮在一起,怎么處理呢

    阿里狗16.6,層疊表能打開,但是打開后內容縮在一起,調整間距之后,關閉在打開還是縮在一起,怎么處理呢 試過初始化窗口,重新破解,還有重新加載補丁,都不行
    發表于 02-13 11:01

    回望2025:與162萬開發者一起,讓AI硬件觸手可及

    設備中。今天,我們想和大家一起回顧這年的成長,也分享我們對未來的期待。2025,我們跟開發者一起完成了1、開發者生態持續壯大截至四季度末,涂鴉平臺累計注冊開發者超
    的頭像 發表于 02-12 18:59 ?171次閱讀
    回望2025:與162萬開發者<b class='flag-5'>一起</b>,讓AI硬件觸手可及

    打擊:下代網絡疑云

    并非簡單的好或壞,它更像面鏡子,映照出技術背后的權力與責任
    的頭像 發表于 12-02 12:47 ?6592次閱讀
    星<b class='flag-5'>鏈</b>打擊:下<b class='flag-5'>一</b>代網絡疑云

    N9H20如何將 SPI 閃存與非作系統 BSP 一起使用?

    N9H20如何將 SPI 閃存與非作系統 BSP 一起使用?
    發表于 09-01 08:27

    N9H20如何將非作系統 NVTFAT 與 SPI 閃存一起使用?

    N9H20如何將非作系統 NVTFAT 與 SPI 閃存一起使用?
    發表于 09-01 06:38

    光纖能與電線一起走嗎

    光纖與電線在特定條件下可以一起布線,但需嚴格遵守安全規范和物理隔離要求,以下是詳細分析: 、光纖與電線的物理特性差異 光纖 傳輸介質:以光信號傳輸數據,不導電,因此不受電磁干擾(EMI
    的頭像 發表于 07-14 10:40 ?7914次閱讀

    尋開發伙伴 一起搞細胞電阻儀,有興趣的朋友來聊聊!

    尋開發伙伴 一起搞細胞電阻儀,有興趣的朋友來聊聊!
    發表于 07-10 15:51

    是否可以將客戶端控件與CYW920706WCDEVAL一起使用?

    是否可以將客戶端控件與CYW920706WCDEVAL一起使用? 我想用它來發現藍牙 BR/EDR,然后將其與其他設備配對。 有客戶端控制的下載鏈接或文檔嗎? 另外,你有 AIROC Connect 藍牙應用程序的文檔嗎?
    發表于 07-04 07:50

    無法將Jlink調試器與CYBT263065EVAL COOLDIM_PRG_BOARD連接在一起怎么解決?

    我無法將 Jlink 調試器與 CYBT263065EVAL COOLDIM_PRG_BOARD連接在一起
    發表于 07-03 06:24

    技術故障率降低20%,關鍵在于這套IoT軟硬件體化方案...

    設備運行不穩定、現場環境惡劣、網絡集成困難?一起來看看這個案例怎么破局
    的頭像 發表于 07-02 10:48 ?409次閱讀
    技術故障率降低20%,關鍵在于這套IoT軟硬件<b class='flag-5'>一</b>體化方案...

    CyU3PDeviceGpioOverride是否僅與LPP引腳一起使用?

    CyU3PDeviceGpioOverride 是否僅與 LPP 引腳一起使用? 如果使用 CyU3PDeviceGpioOverride,則 io_cfg.gpioSimpleEn 跳過還是反之亦然?
    發表于 05-15 07:33

    如何將FX3與WSL(Linux 的 Windows 子系統)一起使用?

    如何將 FX3 與 WSL(Linux 的 Windows 子系統)一起使用? 我在 /dev/ 中找不到任何設備 我有許多項目在 Windows 上使用VISUAL STUDIO項目進行操作,因此請驗證該設備是否在 Windows 上運行。
    發表于 05-06 07:11

    TVS選型的般步驟

    上文我們了解了TVS管的定義和參數,那么今天我們再一起看看TVS管應該如何選型。
    的頭像 發表于 03-17 16:10 ?2517次閱讀
    TVS選型的<b class='flag-5'>一</b>般步驟

    屏蔽網線可以和電線一起

    屏蔽網線與電線不建議一起走線,原因主要有以下幾點: 電磁干擾:電源線在傳輸電能時會產生電磁場,而屏蔽網線中的導線可能會受到這個電磁場的干擾。這種干擾可能導致屏蔽網線的信號質量下降、速度變慢,甚至無法
    的頭像 發表于 03-07 10:47 ?2055次閱讀