Java – how to separate swing GUI from business logic without using spring, etc
Please note that this is a long post Sorry, but I want to clarify one thing:
I want to know how to separate swing GUI from presentation and business logic for a long time In my work, I must use a small swing dialog box to implement 3 MD excel export for some data to configure export We don't use a framework like spring, so I have to implement it myself
I want to completely separate the GUI from business logic, which has the following precise follow-up tasks:
>Tell BL to start working from Gui > report progress from BL to Gui > report logging from BL to Gui > delegate BL results to GUI
Of course, GUI should not pay attention to BL implementation, and vice versa I have created several interfaces for all the above tasks, such as g. progresslistener, logmessagelistener, jobdonelistener, etc., which are dismissed by business logic For example, if the business logic wants to tell logging, it calls
fireLogListeners("Job has been started");
The class implementing the public interface loglistener is attached to BL and will now notify the log message about "job started" All these listeners are implemented by the GUI itself, which generally looks like this:
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. }
The GUI and BL create class simply attaches the GUI (as the interface for all these listeners) to BL, which looks like this:
exportJob.addProgressListener(uiDialog); exportJob.addLogListener(uiDialog); exportJob.addJobFinishedListener(uiDialog); exportJob.start();
I'm very uncertain about this now because it looks strange because of all the newly created listener interfaces What do you have in mind? How to separate swing GUI components from BL?
Editor: for better demonstration purposes, I created a demo workspace file - upload. Exe in eclipse net/download-9065013/exampleWorkspace. zip. HTML I also pasted it into Pastebin, but it's best to import those classes in eclipse, a lot of code http://pastebin.com/LR51UmMp
Solution
Some things.
I have no uidialog code in the exportfunction class The entire execution method should be just the code in the main class The responsibility of exportfunctions is' export ', not' 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. Invoker() is not required)
You seem to be over engineering I don't know why you expect many threads to run at the same time When you press the button, you only want one thread to run correctly? Then there is no need to have an actionperformedlistener array
Not this:
button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (startConditionsFulfilled()) { fireActionListener(ActionPerformedListener.STARTJOB); } } });
Why not?
final ExportJob exportJob = new ExportJob(); exportJob.addJobFinishedListener(this); exportJob.addLogListener(this); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { exportJob.start(); } });
In this way, you can get rid of the export function that is really used for any purpose
You seem to have a large audience Unless you really need them, I won't disturb them and keep them as simple as possible
Substitute:
Thread.sleep(1000); fireLogListener("Excel Sheet 2 created"); Thread.sleep(1000);
Only:
Thread.sleep(1000); log("Excelt Sheet 1 created"); Thread.sleep(1000);
The log is:
private void log(final String message) { ((DefaultListModel<String>) list.getModel()).addElement(message); }
So you can keep it simple and clean
The GUI should not know BL, but BL somehow has to tell the GUI what to do You can use a lot of interfaces for infinite abstraction, but in 99.99% of applications, this is not necessary, especially when your application looks quite simple
Therefore, although the code you write is very good, I will try to simplify and reduce interfaces It can't guarantee so many projects