- Hands-On Reactive Programming with Clojure
- Konrad Szydlo Leonardo Borges
- 283字
- 2021-07-02 14:03:51
The Observer design pattern
Another similarity that the keen reader may have noticed is with the Observer design pattern. It is mainly used in object-oriented applications as a way for objects to communicate with each other without having any knowledge of who depends on its changes.
In Clojure, a simple version of the Observer pattern can be implemented using watches:
(def numbers (atom [])) (defn adder [key ref old-state new-state] (print "Current sum is " (reduce + new-state))) (add-watch numbers :adder adder)
We will start by creating our program state, which in this case is an atom holding an empty vector. Next, we will create a watch function that knows how to sum all numbers in numbers. Finally, we will add our watch function to the numbers atom under the :adder key (useful for removing watches).
The adder key conforms with the API contract required by add-watch and receives four arguments. In this example, we only care about new-state.
Now, whenever we update the value of numbers, its watch will be executed, as demonstrated in the following code:
(swap! numbers conj 1) ;; Current sum is 1 (swap! numbers conj 2) ;; Current sum is 3 (swap! numbers conj 7) ;; Current sum is 10
The highlighted lines indicate the result that is printed on the screen each time we update the atom.
Though useful, the Observer pattern still requires some amount of work in setting up the dependencies and the required program state, in addition to being hard to compose.
That being said, this pattern has been extended and is at the core of one of the Reactive Programming frameworks we will look at later in this book, Microsoft's Reactive Extensions (Rx).
- 陳衛(wèi)川中回醫(yī)發(fā)微
- 寬體探測器CT臨床應(yīng)用
- 食品毒理學(xué)
- 心理腫瘤學(xué):為癌癥患者及其家庭提供心理學(xué)支持
- 臨床神經(jīng)遺傳病學(xué)
- 醫(yī)院醫(yī)療質(zhì)量標(biāo)準(zhǔn)化管理手冊
- Practical PowerShell Security and Compliance Center
- 鑲牙就醫(yī)指南
- 冠心病及急性心肌梗死中醫(yī)臨床辨證標(biāo)準(zhǔn)及防治指南
- 全瓷修復(fù)技術(shù)(第2版)
- 牙齒健康與口腔保健手冊
- 紫外線皮膚科實(shí)戰(zhàn)口袋書
- 胎兒磁共振影像診斷學(xué)
- Mastering Blockchain Programming with Solidity
- 你好,我是阿斯伯格員工