【问题标题】:Clojure defrecord and private fieldsClojure defrecord 和私有字段
【发布时间】:2011-08-13 18:42:48
【问题描述】:

我经常使用 Martin Fowler 的 Presentation Model 模式来实现我的 Java swing GUI。

这是一个例子:

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

interface MainView {
    void configurationButtonAddActionListener(ActionListener actionListener);

    void directoryLabelSetText(String text);

    ListModel fileListGetModel();

    void setVisible(final boolean visible);
}

class MainFrame
        extends JFrame
        implements MainView {
    private final JButton configurationButton = new JButton("Configuration...");
    private final JLabel directoryLabel = new JLabel();
    private final JList fileList = new JList();

    public MainFrame(final String title) {
        super(title);

        final JPanel mainPanel = new JPanel(new BorderLayout());
        add(mainPanel);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));

        mainPanel.add(directoryLabel, BorderLayout.NORTH);
        mainPanel.add(new JScrollPane(fileList));
        mainPanel.add(configurationButton, BorderLayout.SOUTH);

        setSize(800, 600);
        setLocationRelativeTo(null);
    }

    @Override
    public void configurationButtonAddActionListener(final ActionListener actionListener) {
        configurationButton.addActionListener(actionListener);
    }

    @Override
    public void directoryLabelSetText(final String text) {
        directoryLabel.setText(text);
    }

    @Override
    public ListModel fileListGetModel() {
        return fileList.getModel();
    }
}

然后可以将接口传递给负责处理视图上的所有操作的演示者类。可以将模拟版本传递给演示者进行测试,并且视图非常简单,理论上不需要进行单元测试。

我正在尝试使用 defrecord 在 Clojure 中做类似的事情:

(ns mainframe
 (:gen-class)
 (:import
   [java.awt BorderLayout]
   [javax.swing JButton JFrame JLabel JList JPanel JScrollPane]))

(if *compile-files*
  (set! *warn-on-reflection* true))

(defprotocol MainView
  (directory-label-set-text [this text])
  (set-visible [this visible]))

(defrecord mainframe [^JFrame frame
                      directory-label
                      file-list
                      configuration-button]
  MainView
  (directory-label-set-text [this text]
    (.setText directory-label text))
  (set-visible [this visible]
    (.setVisible frame visible)))

(defn create-main-frame
  [title]
  (let [directory-label (JLabel.)

        file-list (JList.)

        configuration-button (JButton. "Configuration...")

        main-panel (doto (JPanel. (BorderLayout.))
                     (.add directory-label BorderLayout/NORTH)
                     (.add (JScrollPane. file-list))
                     (.add configuration-button BorderLayout/SOUTH))

        frame (doto (JFrame.)
                (.setTitle title)
                (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
                (.add main-panel)
                (.setSize 800 600)
                (.setLocationRelativeTo nil))]
    (mainframe. frame directory-label file-list configuration-button)))

我可以使用defprotocol 和defrecord 来完成界面和“类”的唯一方法。有没有更好的办法?有没有办法让defrecord 中包含组件(JButton、JLabel、JList)的“字段”私有?我不喜欢暴露实现细节。

【问题讨论】:

    标签: swing clojure presentation-model


    【解决方案1】:

    对于这些实现,您可能需要deftype 而不是defrecord。 defrecord 更多是关于数据,而deftype 用于实现某些接口背后的细节。这听起来有点模糊,我知道,但这是我对http://clojure.org/datatypes 的解释。我认为你的框架属于第二类。

    我不会花太多时间试图隐藏事情。不要触摸类型字段(除非在接口函数内部)。仅使用接口函数与类型进行交互。然后,该领域在技术上是私有的还是公共的并不重要。 (再次:参见http://clojure.org/datatypes,关于意见的部分)

    【讨论】:

    • deftype 和 defrecord 之间的主要区别之一是 defrecord 自动为您提供 equals() 和 java.lang.Comparable (IIRC) 的默认实现,如果您尝试实现新的数据类型。另一方面,deftype 给你一个完全空白的对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多