差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 | ||
java:java8:exception:observerexceptionhandlingoflambda [2018/02/04 14:35] tony |
java:java8:exception:observerexceptionhandlingoflambda [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== Observer使用Lambda寫法進行通知的例外處理 (Working) ====== | + | ====== Observer使用Lambda寫法對通知的例外處理 ====== |
===== Introduction ===== | ===== Introduction ===== | ||
這是一段常見的Observer寫法的程式碼,FileWatcher是Subject負責確認檔案是否修改,而IFileChangedListener是Observer接收檔案改變通知;當檔案改變時,FileWatcher會執行doOnChange通知各個IFileChangedListener: | 這是一段常見的Observer寫法的程式碼,FileWatcher是Subject負責確認檔案是否修改,而IFileChangedListener是Observer接收檔案改變通知;當檔案改變時,FileWatcher會執行doOnChange通知各個IFileChangedListener: | ||
行 57: | 行 57: | ||
</code> | </code> | ||
接下來就是考慮重複使用的議題。 | 接下來就是考慮重複使用的議題。 | ||
+ | ===== Wrapper The Exception Handling ===== | ||
+ | 為了重複使用,我們可以將處理方式透過Wrapper方式實作並集中於Utility類別: | ||
+ | <code java> | ||
+ | public class Errors { | ||
+ | static private Logger logger = LoggerFactory.getLogger(Errors.class); | ||
+ | public static <T> Consumer<T> logException(Consumer<T> operation){ | ||
+ | return i -> { | ||
+ | try { | ||
+ | operation.accept(i); | ||
+ | } catch (Exception e) { | ||
+ | logger.warn("", e); | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | listeners.forEach(Errors.logException(i->i.update(newContent))); | ||
+ | </code> | ||
+ | 上面這種寫法只能針對unchecked exception,假如是checked exception,可以透過Functional Interface做自己的Consumer: | ||
+ | <code java> | ||
+ | @FunctionalInterface | ||
+ | public interface ThrowingConsumer<T, E extends Exception> { | ||
+ | void accept(T t) throws E; | ||
+ | } | ||
+ | |||
+ | public static <T> Consumer<T> logException(ThrowingConsumer<T, Exception> operation){ | ||
+ | return i -> { | ||
+ | try { | ||
+ | operation.accept(i); | ||
+ | } catch (Exception e) { | ||
+ | logger.warn("", e); | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | </code> | ||
+ | ===== Apply durian - Error class ===== | ||
+ | 假如不介意相依於第三方函式庫,可以參考[[https://github.com/diffplug/durian|durian]]。它針對lambda的functional interface提供了log、rethrow或者自訂義等例外處理方式,這部分之後我有時間會特別介紹。 | ||
===== Reference ===== | ===== Reference ===== | ||
* [[http://www.baeldung.com/java-lambda-exceptions|Exceptions in Java 8 Lambda Expressions]] | * [[http://www.baeldung.com/java-lambda-exceptions|Exceptions in Java 8 Lambda Expressions]] | ||
+ | * [[https://github.com/diffplug/durian|Github - durian]] | ||
+ | ====== ====== | ||
+ | ---- | ||
+ | \\ | ||
+ | ~~DISQUS~~ |