
(require '[clojure.string :as str])
;; Clojure has no inheritance; the nearest thing to "adapter is-a adaptee"
;; is teaching the adaptee's own type the target protocol, with no wrapper
;; Target
(defprotocol IText
(get-text [this])) ; Request()
;; Adapter — the adaptee type itself gains Request()
(extend-type clojure.lang.PersistentVector
IText
(get-text [rows] (str/join "\n" rows))) ; delegates to SpecificRequest()
;; Client — an ordinary vector now answers the target protocol
(def rows ["line 1" "line 2"])
(println (get-text rows))
;; line 1
;; line 2
| Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. |