Clojure 模型-视图-控制器 (MVC) 设计
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您拥有一阶函数、宏(代码即数据)和并发持久数据结构时,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.
在 Clojure 中,您当然可以执行 MVC,但我建议使用 Clojure 引用上的监视来实现它。
代码如下:
另外,如果您在 Clojure 中构建 GUI,那么可能值得查看一些现有的 Swing 库包装器,例如:
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.: