静音设计模式
您最近是否遵循Mute-Design-Pattern?編寫了大量代碼? 例如
try {complex();logic();here(); } catch (Exception ignore) {// Will never happen heheSystem.exit(-1); }Java 8有一個更簡單的方法!
只需將這個非常有用的工具添加到您的Utilities或Helper類:
public class Helper {// 18395 lines of other code here@FunctionalInterfaceinterface CheckedRunnable {void run() throws Throwable;}public static void mute(CheckedRunnable r) {try {r.run();}catch (Throwable ignore) {// OK, better stay safeignore.printStackTrace();}}// 37831 lines of other code here }現(xiàn)在,您可以將所有邏輯包裝在這個漂亮的小包裝中:
mute(() -> {complex();logic();here(); });做完了!
更好的是,在某些情況下,您可以使用方法引用
try (Connection con = ...;PreparedStatement stmt = ...) {mute(stmt::executeUpdate); }翻譯自: https://www.javacodegeeks.com/2016/02/mute-design-pattern.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
- 上一篇: 送好是什么意思 送好解释
- 下一篇: JAXB和Log4j XML配置文件