I want to make GUI applications such that the non-GUI code is independent from the GUI one (let's say I'll use Java 8 and Swing for example). The goal is that I could hypothetically change the GUI API/library, or the kind of UI, for example shifting from a GUI to a console output, without modifying the non-GUI code (also in observance of the Open/Closed principle).
In particular, my question is about how to handle exception messages so that they can be output to any UI: let's say that I have try/catch blocks in my non-GUI code, like this (in the example I'm using Java but the language is irrelevant):
try {
do something;
} catch (AnException e) {
System.err.println("Exception message");
}
Obviously the println wouldn't be executed in the GUI, so the user would be unaware of the message. My goal is to show the exception message through the GUI. A simple way would be to swap the println
for a GUI print method, but doing this my code wouldn't be independent from the GUI. How to solve this problem? Which is the standard approach (if there's such a thing) to deal with this problem?
The question can be generalized to any kind of output message in non-interface-related code.