
(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
| Преобразует интерфейс класса в другой интерфейс, ожидаемый клиентами. Обеспечивает совместную работу классов, которая была бы невозможна без данного паттерна из-за несовместимости интерфейсов. |