Clojure Model-View-Controller (MVC) design

2019-03-15 15:03发布

I am writing a Desktop GUI application in Clojure using Java Swing. Normally when working with Java I will design the application according to a MVC design pattern using the Observer pattern as well. This way the view is decoupled from the model and changes in either do not affect each other, making changes easier further along.

I was wondering if Clojure has a better approach to this problem than normal MVC and Observer design pattern? I'm new to functional programming so i'm not sure how I can make the model separate from the view. I require this as the application will be developed iteratively and there may be challenging requirements that come along further down the line.

Would appreciate any help.

Thanks,

Adam

2条回答
姐就是有狂的资本
2楼-- · 2019-03-15 15:14

In Clojure you can certainly do MVC, but I'd suggest implementing it using watches on Clojure references.

Code would be something like:

; 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))))

Also if you are building a GUI in Clojure, it may well be worth taking a look at some of the existing Swing library wrappers, e.g.:

  • Clarity - has a nice DSL for defining UI elements
  • Seesaw - possibly the most mature wrapper for Swing
  • clj-swing
查看更多
你好瞎i
3楼-- · 2019-03-15 15:18

A lot of the design patterns from the java MVC world get a bit silly when you have first order functions, macroes (code-as-data), and concurrent persistent data structures. for instance the "observer pattern" is basically just an agent with some watches set. It goes from being a pattern to a function call.

if you store the state (model) in a ref or agent and make your view a function (in the functional programming sense of the word) that displays that state; while making your controller a function (again in the FP sense of the word) that produces a new state given the old state and some new input then the MVC model falls out very nicely.

it s bit dated, but Stuart Sierra's grid bag layout post really helped me get started in this area.

查看更多
登录 后发表回答