non*_*com 10 protocols clojure
在Clojure中,我希望有一个协议,其中一些方法具有默认实现,而一些方法具有自定义实现.第一个用于配置后者.这是一个例子:
(defprotocol Saving
(save [this] "saves to mongodb")
(collection-name [this] "must return a string representing the associated MongoDB collection"))
;Default implementation
(extend-type Object
Saving
; the `save` method is common for all, so it is actually implemened here
(save [this] (mc/insert (collection-name [this]) this))
; this method is custom to every other type
(collection-name [this] "no_collection"))
;Particular implementations
(defrecord User
[login password]
Saving
(collection-name [this] "users"))
(defrecord NewsItem
[text date]
Saving
(collection-name [this] "news_items"))
Run Code Online (Sandbox Code Playgroud)
但是,它不会以这种方式工作.即使调用collection-name一个User或NewsItem实例返回一个正确的集合字符串,调用save它们会导致一个AbstractMethodError.我怎样才能用Clojure实现这个琐碎的OO形目标?
Ank*_*kur 12
使保存功能成为正常功能:
(defn save [obj] (mc/insert (collection-name obj) obj))
Run Code Online (Sandbox Code Playgroud)
该协议应该只有 collection-name
(defprotocol Saving
(collection-name [this] "must return a string representing the associated MongoDB collection"))
Run Code Online (Sandbox Code Playgroud)
然后,每个想要"保存"的对象都可以实现此协议.
记住:OO风格经常隐藏明显简单的东西:)
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |