Clojure 模型-视图-控制器 (MVC) 设计

发布于 2024-12-27 18:45:32 字数 300 浏览 2 评论 0原文

我正在使用 Java Swing 在 Clojure 中编写桌面 GUI 应用程序。通常,在使用 Java 时,我会根据 MVC 设计模式并使用观察者模式来设计应用程序。通过这种方式,视图与模型分离,并且两者的更改不会相互影响,从而使进一步的更改变得更加容易。

我想知道 Clojure 是否有比普通 MVC 和观察者设计模式更好的方法来解决这个问题?我是函数式编程的新手,所以我不确定如何使模型与视图分离。我需要这样做,因为应用程序将被迭代开发,并且可能会进一步出现具有挑战性的要求。

将不胜感激任何帮助。

谢谢,

亚当

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2025-01-03 18:45:32

当您拥有一阶函数、宏(代码即数据)和并发持久数据结构时,Java MVC 世界中的许多设计模式都会变得有点愚蠢。例如,“观察者模式”基本上只是一个设置了一些手表的代理。它从模式转变为函数调用。

如果您将状态(模型)存储在 ref 或代理中,并使您的视图成为显示该状态的函数(在函数式编程的意义上);当让你的控制器成为一个函数(同样是 FP 意义上的函数),在给定旧状态和一些新输入的情况下生成新状态时,MVC 模型就会很好地发挥作用。

它有点过时了,但是 Stuart Sierra 的 网格包布局帖子 确实对我有帮助开始在这个领域。

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.

や三分注定 2025-01-03 18:45:32

在 Clojure 中,您当然可以执行 MVC,但我建议使用 Clojure 引用上的监视来实现它。

代码如下:

; 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 库包装器,例如:

  • Clarity - 有一个很好的 DSL,用于定义 UI 元素
  • Seesaw - 可能是最成熟的Swing 的包装器
  • clj-swing

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.:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文