我经常使用马丁·福勒(Martin Fowler)实施我的Java Swing Guis 演示模型 图案。

这是一个示例:

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:

(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)))

我可以使用界面的唯一方法是使用“类” defprotocoldefrecord. 。有没有更好的办法?有什么方法可以在 defrecord 包含私有组件(jbutton,jlabel,jlist)?我不喜欢公开实施细节。

有帮助吗?

解决方案

对于这些实施方式,您可能想要 deftype 代替 defrecord. defrecord 更多关于数据,而 deftype 用于实现某些接口背后的小小的剪力。我知道这听起来有点模糊,但这是我对 http://clojure.org/datatypes. 。我认为您的框架属于第二类。

我不会花太多时间尝试隐藏东西。不要触摸类型字段(除非在接口函数内部)。仅使用接口函数与类型交互。那么该领域在技术上是私人还是公共都没关系。 (再次:参见。 http://clojure.org/datatypes, ,关于意见的部分)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top