在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"))
然而,它不会以这种方式工作。即使在collection-name
或User
实例上调用NewsItem
返回正确的集合字符串,在其上调用save
也会导致AbstractMethodError
。我怎么能用Clojure来实现这个琐碎的OO形目标呢?
答案 0 :(得分:12)
使保存功能成为正常功能:
(defn save [obj] (mc/insert (collection-name obj) obj))
协议应该只有collection-name
(defprotocol Saving
(collection-name [this] "must return a string representing the associated MongoDB collection"))
然后,每个想要“保存”的对象都可以实现此协议。
请记住:OO风格经常隐藏明显简单的事情:)