Open rhit-hueyee opened 7 months ago
Upon further research there is some further nuance that I have discovered. There are two main issues I'm at right now. For starters here's the "base" singleton.
public class Settings {
private static Settings instance;
private Settings(){}
public static Settings getInstance() {
if (instance == null) {
instance = new Settings();
}
return instance;
}
}
Problem 1: This implementation doesn't work well with threads. As of right now I have very little experience working with threads, especially in Java. This article has information about threading with Singletons: https://www.baeldung.com/java-singleton
Problem 2: Adding a singleton adds more difficulty onto Unit Testing. My best guess why is because for each test, I most likely have to configure the settings for each state. I imagine using the application in Spanish (using the language example) and then running the unit tests. Let's say I need to test English, Spanish, and Japanese. Not only do I have to change the settings for each test, I would then have to change back to Spanish when I am done. This is a simple example, but I can imagine how things can spiral out of control if I am not careful. Problem for future me >﹏<
I added another change to Settings, but I think it might be for the best to place the feature in another class.
I also have Settings help deal with Observers like so
private static final List<UIChangeListener> UI_LISTENERS = new ArrayList<>();
public static void addUIChangeListener(UIChangeListener listener) {
UI_LISTENERS.add(listener);
}
public static void removeUIChangeListener(UIChangeListener listener) {
UI_LISTENERS.remove(listener);
}
public static void notifyUIChange() {
for (UIChangeListener listener : UI_LISTENERS) {
listener.updateLangUI();
}
}
public void echoUpdateUI() {
notifyUIChange();
}
but I think it might be wiser to move this to another class in the future
I am thinking that to implement global settings, use of the Singleton Design Pattern would be nice