Patterns / Structural patterns
image

(require '[clojure.string :as str])

;; Adapter by composition folds into a closure: the adaptee is captured,
;; the adapter is the one function the client wanted

;; Adaptee
(defn string-list [] (atom []))
(defn add-row [sl value] (swap! sl conj value))
(defn get-string [sl] (str/join "\n" @sl))    ; SpecificRequest()

;; Adapter — a get-text function closed over the adaptee
(defn text-adapter [sl]
  (fn [] (get-string sl)))                    ; Request()

;; Client
(def rows (string-list))
(add-row rows "line 1")
(add-row rows "line 2")
(def get-text (text-adapter rows))
(println (get-text))
;; 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.