Clojure Model-View-Controller (MVC) design
H

2

10

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

Hemorrhage answered 17/1, 2012 at 21:42 Comment(2)
In case you are doing a lot of Swing with Clojure you might find seesaw useful.Compressed
I second seesaw. Very functional and consistent interface.Systemic
L
11

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.

Luana answered 17/1, 2012 at 21:51 Comment(0)
G
8

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
Gelation answered 18/1, 2012 at 1:15 Comment(1)
Consider adding Seesaw (github.com/daveray/seesaw) into the listed libraries.Kranz

© 2022 - 2024 — McMap. All rights reserved.