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
In Clojure you can certainly do MVC, but I'd suggest implementing it using watches on Clojure references.
Code would be something like:
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.:
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.