简体   繁体   中英

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 ie 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?

PS: 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.

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, eg

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, ie the listeners should take care to use SwingUtilities.invokeLater or invokeAndWait in order to update the GUI.

Maybe this answer will look a little old-school, but what about plain old IPCs?

Make the GUI instantiate the core as a separate thread, and communicate only via message queues (java.util.concurrent.*). Use them to send list of files, events, progress reports and virtually anything.

Then you can ship several interfaces, as long as all interfaces know the entry point for the core, you're in business.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM