;; 协议列出类型必须响应的方法
(defprotocol Car
(start-engine [this])
(stop-engine [this]))
;; 记录本身不可变,所以会变化的状态放在 atom 里
(defrecord SportCar [started]
Car
(start-engine [this]
(if @started
false
(do (println "start engine")
(reset! started true)
true)))
(stop-engine [this]
(println "stop engine")
(reset! started false)))
(def car (->SportCar (atom false)))
(println "started is" (start-engine car))
;; 第二次调用时引擎已在运行
(println "started is" (start-engine car))
(stop-engine car)