加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Clojure GUI编程很难

发布时间:2020-12-16 09:14:49 所属栏目:安全 来源:网络整理
导读:我正在使用Swing GUI编写一个实用程序.我试图用Martin Fowler的 Presentation Model来方便测试.我的应用程序将使用java.util.prefs.Preferences(即:主窗口位置和大小)自动存储多个用户首选项.我在周末度过了几个小时,尝试创建一个Clojure模拟的Preferences
我正在使用Swing GUI编写一个实用程序.我试图用Martin Fowler的 Presentation Model来方便测试.我的应用程序将使用java.util.prefs.Preferences(即:主窗口位置和大小)自动存储多个用户首选项.我在周末度过了几个小时,尝试创建一个Clojure模拟的Preferences API(使用 EasyMock),以便我可以测试我的演示者代码,但无法使其工作. Clojure使用非OO风格的GUI编程很难为长时间的OO编程人员使用.我觉得如果我可以发现/开发这些东西的模式(嘲笑,“接口”的视觉“类”等),我可以继续使用相同的模式在整个应用程序的其余部分.

我也在Scala开发相同的应用程序来比较编程模式,并且发现它更直观,尽管我试图以相当严格的功能样式来使用Scala(不包括,当然也可以调用Java类,例如Swing API – 它将在Clojure版本中具有相同的可变性问题,但当然也将是单线程的).

在我的Scala代码中,我创建一个名为MainFrame的类,扩展了JFrame并实现了特征MainView. MainView将所有JFrame调用公开为抽象方法,我可以在一个模拟对象中实现:

trait LabelMethods {
  def setText(text: String)
  //...
}

trait PreferencesMethods {
  def getInt(key: String,default: Int): Int
  def putInt(key: String,value: Int)
  //...
}

trait MainView {
  val someLabel: LabelMethods
  def addComponentListener(listener: ComponentListener)
  def getLocation: Point
  def setVisible(visible: Boolean)
  // ...
}

class MainFrame extends JFrame with MainView {
  val someLabel = new JLabel with LabelMethods
  // ...
}

class MainPresenter(mainView: MainView) {
  //...
  mainView.addComponentListener(new ComponentAdaptor {
    def componentMoved(ev: ComponentEvent) {
      val location = mainView.getLocation
      PreferencesRepository.putInt("MainWindowPositionX",location.x)
      PreferencesRepository.putInt("MainWindowPositionY",location.y)
  }
  mainView.someLabel.setText("Hello")
  mainView.setVisible(true)
}

class Main {
  def main(args: Array[String]) {
    val mainView = new MainFrame
    new MainPresenter(mainView)
  }
}

class TestMainPresenter {
  @Test def testWindowPosition {
    val mockPreferences = EasyMock.mock(classOf[PreferencesMethods])
    //... setup preferences expectation,etc.
    PreferencesRepository.setInstance(mockPreferences)

    val mockView = EasyMock.createMock(classOf[MainView])
    //... setup view expectations,etc.
    val presenter = new MainPresenter(mockView)
    //...
  }
}

我使用伪单(包括setInstance,以便模拟可以替换“真实”版本)为首选项,因此细节未显示.我知道蛋糕的样式,但在这种情况下,我发现我的一个更容易使用.

我在Clojure中做了类似的代码.有什么好的开源项目的例子做这样的事情吗?我已经阅读了几本关于Clojure(编程Clojure,Clojure的乐趣,实践Clojure)的书,但没有看到这些问题被处理.我也研究了Rich Hickey的ants.clj,但是在这个例子中他使用Swing是非常基本的.

解决方法

你看过这篇文章了吗? http://stuartsierra.com/2010/01/08/agents-of-swing

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读