dimanche 21 mai 2023

Is having a Static Map like this a bad practice?

I want to have some important classes available through out the app, like so:

private static final HashMap<Class<?>, Object> _globals = new HashMap<>();

public MainClass(){
     _stateManager = new StateManager();
     _inputManager = new InputManager();
     _toolManager = new ToolManager();
     _viewManager = new ViewManager();
     _globals.put(StateManager.class, _stateManager);
     _globals.put(InputManager.class, _inputManager);
     _globals.put(ToolManager.class, _toolManager);
     _globals.put(ViewManager.class, _viewManager);
}

From what I've been researching, static vars are always discouraged, but I feel like this might be ok, because I'm not actually keeping any static state. The main purpose of _globals if to have it available for example in the class TextFieldController without having to pass it to the constructors, from the main class all the way to there, going through classes that might not need it.

Is this ok or is there any problems i'm not seeing ?

Aucun commentaire:

Enregistrer un commentaire