官术网_书友最值得收藏!

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).

主站蜘蛛池模板: 南雄市| 乌海市| 桓仁| 汶上县| 隆尧县| 阿图什市| 郴州市| 获嘉县| 黔江区| 乌鲁木齐市| 青阳县| 慈利县| 宜春市| 康乐县| 岱山县| 招远市| 盐池县| 威宁| 明水县| 昌图县| 江安县| 西华县| 和龙市| 玛纳斯县| 张家川| 防城港市| 炉霍县| 瓮安县| 逊克县| 天水市| 丽水市| 莎车县| 雷州市| 锡林浩特市| 昂仁县| 东山县| 福建省| 冷水江市| 手游| 江华| 拉萨市|