To support a modular and maintainable desktop application interface—inspired by IDEs like Eclipse—it's useful to manage key UI components as globally accessible singletons. These include the main window, primary workspace panel, document tree, message console, and potentially other future extensions such as a map overview ("eagle-eye") view.
Given that components like the main frame, working area, file explorer, and status panel are expected to be unique instances throughout the application lifecycle, a centralized singleton registry simplifies access and promotes loose coupling.
A dedicated module named QHService is introduced, containing a package SingleService. Within this package, two core classes are defined:
- SingleInstanceKey: A static key container that enumerates identifiers for each singleton component.
- SingleInstance: A registry that stores and retrieves singleton instances using these keys.
The key definitions are implemented as integer constants:
package SingleService;
public class SingleInstanceKey {
public static final int SERVICE_MANAGER = 0;
public static final int MAIN_FRAME = 1;
public static final int WORKSPACE_PANEL = 2;
public static final int FILE_TREE_PANEL = 3;
public static final int MESSAGE_CONSOLE = 4;
}
The registry uses a fixed-size array (sufficient for early development) to store instances:
package SingleService;
public class SingleInstance {
private static final Object[] INSTANCE_REGISTRY = new Object[100];
public static void register(int key, Object instance) {
INSTANCE_REGISTRY[key] = instance;
}
public static Object get(int key) {
return INSTANCE_REGISTRY[key];
}
}
This design allows any part of the application to register or retrieve a global component by its symbolic key. For example, after initializing the main window, it can be registered via SingleInstance.register(SingleInstanceKey.MAIN_FRAME, mainFrame), and later accessed from elsewhere without tight dependencies.