java – 如何在不使用Spring等时将Swing GUI与Business Logic分
请注意,这是一篇很长的帖子.对不起,但我想澄清一点:
我想知道如何在很长一段时间内将Swing GUI与Presentation和Business Logic分开. 我想将GUI与Business Logic完全分开,后者具有以下精确的后续任务: >告诉BL从GUI开始工作 当然,GUI不应该注意BL实现,反之亦然. fireLogListeners("Job has been started"); 实现公共接口LogListener的类附加到BL,现在将通知有关“作业已启动”的日志消息. public class ExportDialog extends JDialog implements ProgressListener,LogListener,JobFinishedListener,ErrorListener { @Override public void jobFinished(Object result){ // Create Save File dialog and save exported Data to file. } @Override public void reportProgress(int steps){ progressBar.setValue(progressBar.getValue()+steps); } @Override public void errorOccured(Exception ex,String additionalMessage){ ExceptionDialog dialog = new ExceptionDialog(additionalMessage,ex); dialog.open(); } // etc. } “GUI和BL创建类”只是将GUI(作为所有这些侦听器的界面)附加到BL,它看起来像这样: exportJob.addProgressListener(uiDialog); exportJob.addLogListener(uiDialog); exportJob.addJobFinishedListener(uiDialog); exportJob.start(); 我现在对此非常不确定,因为它看起来很奇怪,因为所有新创建的侦听器接口. 编辑: 解决方法
一些东西.
我在ExportFunction类中没有uiDialog代码.整个执行方法应该只是主类中的代码. ExportFunctions的责任是’导出’而不是’show gui’. public static void main(String[] args) { ExportFunction exporter = new ExportFunction(); final ExportUIDialog uiDialog = new ExportUIDialog(); uiDialog.addActionPerformedListener(exporter); uiDialog.pack(); uiDialog.setVisible(true); } (不需要Swing.invokeLater()) 你好像过度工程了.我不知道为什么你会期望有很多线程同时运行.当您按下按钮时,您只希望一个线程正确运行?然后就不需要有一个actionPerformedListener数组. 而不是这个: button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (startConditionsFulfilled()) { fireActionListener(ActionPerformedListener.STARTJOB); } } }); 为什么不呢: final ExportJob exportJob = new ExportJob(); exportJob.addJobFinishedListener(this); exportJob.addLogListener(this); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { exportJob.start(); } }); 这样你就可以摆脱出真正用于任何目的的ExportFunction. 你似乎有很多听众阵列.除非你真的需要它们,否则我不会打扰它们并保持尽可能简单. 代替 : Thread.sleep(1000); fireLogListener("Excel Sheet 2 created"); Thread.sleep(1000); 只有: Thread.sleep(1000); log("Excelt Sheet 1 created"); Thread.sleep(1000); 日志是: private void log(final String message) { ((DefaultListModel<String>) list.getModel()).addElement(message); } 这样你就可以保持简单和清洁. GUI不应该知道BL,但BL不知何故必须告诉GUI该做什么.您可以使用大量接口无限抽象,但在99.99%的应用程序中,这不是必需的,尤其是您的应用程序看起来相当简单. 因此,虽然您编写的代码非常好,但我会尝试简化并减少接口.它不能保证那么多工程. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |