Clojure Model-View-Controller(MVC)设计 [英] Clojure Model-View-Controller (MVC) design

本文介绍了Clojure Model-View-Controller(MVC)设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Java Swing在Clojure中编写桌面GUI应用程序。通常当使用Java时,我将使用Observer模式根据MVC设计模式设计应用程序。通过这种方式,视图与模型分离,变化不会相互影响,使更改更容易。



我想知道Clojure是否比普通的MVC和Observer设计模式有更好的方法来解决这个问题?我是功能性编程的新功能,所以我不知道如何使模型与视图分开。我需要这个,因为应用程序将被迭代地开发,可能会有更多的挑战性的要求。



感谢任何帮助。



谢谢,



Adam

解决方案

当您有一级功能,宏(代码为数据)和并发持久性数据结构时,来自java MVC世界的许多设计模式有点愚蠢。例如观察者模式基本上只是一些手表设置的代理。它从一个模式转变为一个函数调用。



如果您将状态(模型)存储在引用或代理中,并使您的视图成为显示该状态的功能(在功能编程方面)同时让你的控制器成为一个功能(再次是在FP的意义上)产生一个新的状态给予旧的状态和一些新的输入,那么MVC模型非常好地落下。



它有点过时,但Stuart Sierra的网格包布局帖子真的帮助我开始在这方面。


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

解决方案

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 Model-View-Controller(MVC)设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆