文章出處

前言

 在項目中我們一般會為實際問題域定義領域數據模型,譬如開發VDOM時自然而言就會定義個VNode數據類型,用于打包存儲、操作相關數據。clj/cljs不單內置了ListVectorSetMap等數據結構,還提供deftypedefrecord讓我們可以自定義數據結構,以滿足實際開發需求。

定義數據結構從Data Type和Record開始

 提及數據結構很自然就想起C語言中的struct,結構中只有字段并沒有定義任何方法,而這也是deftypedefrecord最基礎的玩法。
示例

(deftype VNode1 [tag props])
(defrecord VNode2 [tag props])

(def vnode1
  (VNode1. "DIV" {:textContent "Hello world!"}))
;; 或 (->VNode1 "DIV" {:textContent "Hello world!"})

(def vnode2
  (VNode2. "DIV" {:textContent "Hello world!"}))
;; 或 (->VNode2 "DIV" {:textContent "Hello world!"})
;; 或 (map->VNode2 {:tag "DIV", :props {:textContent "Hello world!"}})

 這樣一看兩者貌似沒啥區別,其實區別在于成員的操作上

;; deftype取成員值
(.-tag vnode1) ;;=> DIV
;; defrecord取成員值
(:tag vnode2)  ;;=> DIV

;; deftype修改成員值
(set! (.-tag vnode1) "SPAN")
;; 而 (aset vnode1 "tag" "SPAN"),這種方式不會改變vnode1的值
(.-tag vnode1) ;;=> SPAN

;; defrecord無法修改值,只能產生一個新實例
(def vnode3
  (assoc vnode2 :tag "SPAN"))
(:tag vnode2) ;;=> DIV
(:tag vnode3) ;;=> SPAN

 從上面我們可以看到defrecord定義的數據結構可以視作Map來操作,而deftype則不能。
 但上述均為術,而背后的道則是:
在OOP中我們會建立兩類數據模型:1.編程領域模型;2.應用領域模型。對于編程領域模型(如String等),我們可以采用deftype來定義,從而提供特殊化能力;但對于應用領域模型而言,我們應該對其進行抽象,從而采用已有的工具(如assoc,filter等)對其進行加工,并且對于應用領域模型而言,一切屬性應該均是可被訪問的,并不存在私有的需要,因為一切屬性均為不可變的哦。

Protocol

 Protocol如同Interface可以讓我們實施面對接口編程。上面我們通過deftypedefrecord我們可以自定義數據結構,其實我們可以通過實現已有的Protocol或自定義的Protocol來擴展數據結構的能力。

deftypedefrecord在定義時實現Protocol

;; 定義protocol IA
(defprotocol IA
  (println [this])
  (log [this msg]))

;; 定義protocol IB
(defprotocol IB
  (print [this]
         [this msg]))

;; 定義數據結構VNode并實現IA和IB
(defrecord VNode [tag props]
  IA
  (println [this]
    (println (:tag this)))
  (log [this msg]
    (println msg ":" (:tag this)))
  IB
  (print ([this]
    (print (:tag this)))))

;; 各種調用
(def vnode (VNode. "DIV" {:textContent "Hello!"}))
(println vnode)
(log vnode "Oh-yeah:")
(print vnode)

注意IB中定義print為Multi-arity method,因此實現中即使是僅僅實現其中一個函數簽名,也要以Multi-arity method的方式實現。

(print ([this] (print (:tag this))))

否則會報java.lang.UnsupportedOperationException: nth not supported on this type: Symbol的異常

對已有的數據結構追加實現Protocol

 Protocol強大之處就是我們可以在運行時擴展已有數據結構的行為,其中可通過extend-type對某個數據結構實現多個Protocol,通過extend-protocol對多個數據結構實現指定Protocol。
1.使用extend-type

;; 擴展js/NodeList,讓其可轉換為seq
(extend-type js/NodeList
  ISeqable
  (-seq [this]
    (let [l (.-length this)
          v (transient [])]
      (doseq [i (range l)]
        (->> i
          (aget this)
          (conj! v)))
      (persistent! v))))
;; 使用
(map
  #(.-textContent %)
  (js/document.querySelector "div"))

;; 擴展js/RegExp,讓其可直接作為函數使用
(extend-type js/RegExp
  IFn
  (-invoke ([this s]
    (re-matches this s))))

;; 使用
(#"s.*" "some") ;;=> some

2.使用extend-protocol

;; 擴展js/RegExp和js/String,讓其可直接作為函數使用
(extend-protocol IFn
  js/RegExp
  (-invoke ([this s] (re-matches this s)))
  js/String
  (-invoke ([this n] (clojure.string/join (take n this)))))

;; 使用
(#"s.*" "some") ;;=> some
("test" 2) ;;=> "te"

 另外我們可以通過satisfies?來檢查某數據類型實例是否實現指定的Protocol

(satisfies? IFn #"test") ;;=> true
;;對于IFn我們可以直接調用Ifn?
(Ifn? #"test") ;;=>true

reify構造實現指定Protocol的無屬性實例

(defn user
  [firstname lastname]
  (reify
    IUser
    (full-name [_] (str firstname lastname))))
;; 使用
(def me (user "john" "Huang"))
(full-name me) ;;=> johnHuang

specifyspecify!為實例追加Protocol實現

specify可為不可變(immutable)和可復制(copyable,實現了ICloneable)的值,追加指定的Protocol實現。其實就是向cljs的值追加啦!

(def a "johnHuang")
(def b (specify a
         IUser
         (full-name [_] "Full Name")))

(full-name a) ;;=>報錯
(full-name b) ;;=>Full Name

specify!可為JS值追加指定的Protocol實現

(def a #js {})
(specify! a
  IUser
  (full-name [_] "Full Name"))

(full-name a) ;;=> "Full Name"

總結

 cljs建議對數據結構進行抽象,因此除了List,Map,Set,Vector外還提供了Seq;并內置一系列數據操作的函數,如map,filter,reduce等。而deftype、defrecord更多是針對面向對象編程來使用,或者是面對內置操作不足以描述邏輯時作為擴展的手段。也正是deftype,defrecorddefprotocol讓我們從OOP轉FP時感覺更加舒坦一點。
 另外deftype,defrecord和protocol這套還有效地解決Expression Problem,具體請查看http://www.ibm.com/developerworks/library/j-clojure-protocols/

尊重原創,轉載請注明來自:http://www.cnblogs.com/fsjohnhuang/p/7154085.html ^_^肥仔John


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()