Patterns / Creational patterns
image

;; Prototype needs no cloning: values are immutable, so sharing is safe and
;; a "copy with a change" is assoc onto the sample

;; Prototype
(def square {:shape :square :color "Red"})

;; Client — makes shapes from the prototype
(def square-1 square)                       ; no copy needed at all
(def square-2 (assoc square :color "Blue")) ; a variant of the prototype

(println (:color square-1))    ; Red
(println (:color square-2))    ; Blue
(println (:color square))      ; Red — the prototype is untouched
(println (= square-1 square))  ; true — the same value


Specify the kinds of objects to create using a prototypical instance, and create new objectsby copying this prototype.