我正在使用Java Swing在Clojure中编写桌面GUI应用程序。通常情况下,在使用Java时,我会按照MVC设计模式并使用Observer模式进行应用程序的设计。这样,视图与模型分离,两者之间的更改不会互相影响,使得后续更改更加容易。
我想知道Clojure是否有比普通的MVC和Observer设计模式更好的解决方法?由于我是新手,所以不确定如何将模型与视图分离。我需要这个功能,因为该应用程序将迭代开发,可能会出现进一步挑战性要求。
非常感谢任何帮助。
谢谢,
Adam
我正在使用Java Swing在Clojure中编写桌面GUI应用程序。通常情况下,在使用Java时,我会按照MVC设计模式并使用Observer模式进行应用程序的设计。这样,视图与模型分离,两者之间的更改不会互相影响,使得后续更改更加容易。
我想知道Clojure是否有比普通的MVC和Observer设计模式更好的解决方法?由于我是新手,所以不确定如何将模型与视图分离。我需要这个功能,因为该应用程序将迭代开发,可能会出现进一步挑战性要求。
非常感谢任何帮助。
谢谢,
Adam
; define the model as an immutable structure stored in a ref
(def model (ref (create-my-model)))
; function to update the UI when the model changes
(def update-function [old-model new-model]
(do-whatevever-updates old-model new-model))
; add a watch to the model to call update-function when a change happens
(add-watch model :on-update
(fn [key reference old-state new-state]
(if (not= old-state new-state)
(update-function old-state new-state))))
如果您正在使用Clojure构建GUI,那么看看一些现有的Swing库包装器可能是值得的,例如: