静音设计模式
您最近是否遵循Mute-Design-Pattern?編寫了大量代碼? 例如
try {complex();logic();here(); } catch (Exception ignore) {// Will never happen heheSystem.exit(-1); }Java 8有一個(gè)更簡(jiǎn)單的方法!
只需將這個(gè)非常有用的工具添加到您的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)在,您可以將所有邏輯包裝在這個(gè)漂亮的小包裝中:
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)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
- 上一篇: 送好是什么意思 送好解释
- 下一篇: 中国剩余定理即孙子定理的五种解法