
;; Proxy folds into a delay: the real subject is built at the first deref
;; and never before, so cheap questions cost nothing
;; RealSubject — expensive to make
(defn image [file-name]
(println "loading" file-name)
{:file-name file-name
:draw #(println "draw" file-name)}) ; Request()
;; Proxy — holds the cheap part and a promise of the real one
(defn image-proxy [file-name]
{:file-name file-name
:image (delay (image file-name))})
;; Client
(def p (image-proxy "1.png"))
(println (:file-name p)) ; answered without creating the RealSubject
((:draw @(:image p))) ; now the RealSubject is created
;; 1.png
;; loading 1.png
;; draw 1.png