日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

善用封盖

發布時間:2023/12/3 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 善用封盖 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不久前,在博客文章中 ,我解釋了Groovy中的Closure。 這篇博客文章將解釋一個使用它們的好例子。 最近,我發現自己不得不為服務AJAX請求的大量后端Controller API編寫相同的異常處理邏輯。 就像這樣:

class ApiRugbyPlayerController {JSON getPlayerStats() {try {...// invoke business service method to get player stats} catch (ServiceException serviceException) {// don't care too much about this.// log and move on... } catch (SessionException sessionException) {// clear out session cookies...// send 403 to client...} catch (Exception ex) {throw new ApiException(ex)}}JSON updatePlayerStats() {try {...// invoke business service method to update player stats} catch (ServiceException serviceException) {// don't care too much about this.// log and move on... } catch (SessionException sessionException) {// clear out session cookies...// send 403 to client...} catch (Exception ex) {throw new ApiException(ex)}}JSON queryPlayerStats(){try {...// invoke business service method to query player stats} catch (ServiceException serviceException) {// don't care too much about this.// log and move on... } catch (SessionException sessionException) {// clear out session cookies...// send 403 to client...} catch (Exception ex) {throw new ApiException(ex)}} }

可以看出,這里有一些代碼重復。 本著DRY的精神(請勿重復),最好只定義一次此異常處理邏輯,然后重新使用它。 因此,我定義了以下實用程序方法,該方法實現了異常處理模式并采用了一個閉包,該閉包將為其執行異常處理。

private JSON withExceptionHandling(Closure c) {try {...c.call();} catch (ServiceException serviceException) {// don't care too much about this. // log and move on... } catch (SessionException sessionException) {// clear out session cookies...// send 403 to client ...} catch (Exception ex) {throw new ApiException(ex)}}

我們可以通過用{}包圍代碼來在Groovy中使代碼塊成為閉包。 這意味著我可以將Controller方法內部的邏輯轉換為Closures,并將其傳遞給Utility方法。 而且當我將其傳遞給實用程序方法時,我甚至不需要將其傳遞到()中,因為Groovy不會使您滿意。 因此,這意味著我可以執行所有常見的異常處理,消除代碼膨脹,并且我的Controller API更加整潔。

class ApiRugbyPlayerController {JSON getPlayerStats() {withExceptionHandling {...// invoke business service method to get player stats} }JSON updatePlayerStats() {withExceptionHandling {...// invoke business service method to update player stats} }JSON queryPlayerStats(){withExceptionHandling {...// invoke business service method to query player stats} }private JSON withExceptionHandling(Closure c) {try {...c.call();} catch (ServiceException serviceException) {// don't care too much about this. // log and move on... } catch (SessionException sessionException) {// clear out session cookies...// send 403 to client ...} catch (Exception ex) {throw new ApiException(ex)}} }

所以我們去了。 我們堅持DRY原則,避免了代碼膨脹,并為我們的異常處理提供了專門的場所,確信它可以始終如一地實現。 Groovy閉包的這個例子有點像,但是就像JavaScript中的第二次調用一樣。 如果我們想用Java做類似的事情,那將涉及很多代碼。 我們可以使用類似命令模式的東西,并將它們的執行放入異常處理邏輯中。 您將有更多的去耦,但是您有更多的代碼。 或者,您可以使所有AJAX API輸入一個通用方法(例如Front Controller),但是要在那里輸入通用異常。 同樣,可能,但僅需更多代碼。 在下一次之前,請多保重。

參考: 都柏林技術博客博客上的JCG合作伙伴 Alex Staveley 很好地使用了閉包 。

翻譯自: https://www.javacodegeeks.com/2014/03/good-use-of-closures.html

總結

以上是生活随笔為你收集整理的善用封盖的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。