Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep Java GUI totally separate from Core

I am working on a project. I am trying to implement the core as independent jar file which can be run from command line or even a windows service. The core would be responsible to keep track couple of files and send some notification emails. The question is, what would be the best idea to keep GUI totally independent?

The GUI needs following interation with the core

  • send list of files
  • receive notifications from core regarding how much those files has been processed
  • receive status notifications regarding files i.e. SEND/Processing/Failed etc to be displayed in GUI
  • receive information if there is incoming messages from core

I had this software developed in Delphi and C. C was used to code the core logic and using Windows Messages and Callbacks, i registered Delphi GUI on C dll/service. I am confused how to implement it in java.

  • Observer pattern?
  • Small client/server communication between core and gui?

P.S: The reason i am discussing it here is to learn and explore better design for such softwares when coded in Java. I ain't asking for Observer Pattern documentation or client server architecture. There could be other possible means which i am not aware of. So I am looking forward to any idea, design or framework.

like image 640
Em Ae Avatar asked Oct 09 '22 05:10

Em Ae


1 Answers

Oberserver Pattern is really the correct answer for three out of your 4 use cases.

On the level of your description you might have the following interface implemented by your core:

public interface Core {

    sendFiles(List<File> files);
    registerProgressListener(ProgressListener listener);            
    registerStatusListener(StatusListener listener);
    registerMessageListener(MessageListener listener);
}

The listeners interfaces will look really similar to this one

public interface ProgressListener{
    madeProgress(ProgressEvent)
}

ProgressEvent (and the other Event classes) should be value objects, e.g.

public class ProgressEvent {
    public final double progress;
    public final String fileName;
    public ... // constructor
}

You probably want your core and your gui to run in different threads. Otherwise your GUI would not react to any events while the core is running. Since the core shouldn't know anything about the GUI the handover between threads should be done by the GUI, i.e. the listeners should take care to use SwingUtilities.invokeLater or invokeAndWait in order to update the GUI.

like image 115
Jens Schauder Avatar answered Oct 12 '22 21:10

Jens Schauder