用iis做网站,福建做网站公司排名,湖南建设厅网站证书查询,太仓做网站的公司不久前#xff0c;在博客文章中 #xff0c;我解释了Groovy中的Closure。 这篇博客文章将解释一个使用它们的好例子。 最近#xff0c;我发现自己不得不为服务AJAX请求的大量后端Controller API编写相同的异常处理逻辑。 就像这样#xff1a; class ApiRugbyPlayerControl… 不久前在博客文章中 我解释了Groovy中的Closure。 这篇博客文章将解释一个使用它们的好例子。 最近我发现自己不得不为服务AJAX请求的大量后端Controller API编写相同的异常处理逻辑。 就像这样 class ApiRugbyPlayerController {JSON getPlayerStats() {try {...// invoke business service method to get player stats} catch (ServiceException serviceException) {// dont 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) {// dont 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) {// dont 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) {// dont 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) {// dont 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