3 ms·
YMMV, but the way I prefer doing it is by telling the settings explicitly to the different classes, instead of having all those classes rely on getting it by th
by glassx 15y ago
YMMV, but the way I prefer doing it is by telling the settings explicitly to the different classes, instead of having all those classes rely on getting it by themselves from the singleton (this is where the problem lies).
You can pass the settings in the constructors, with setter methods or have a "state object" that contains the configuration.
For "updating" the states on every class in real time I'd rather rely on explicit state change using the Observer pattern.
EDIT: I'd also like to know what other developers do, or if there's anything wrong with my methods, so I second your question.
- mirkules 15y agoI have been wondering the same, about other developers. I am working on an application that periodically pulls information from a server about a physical object (sorry, I can't be more specific than this, so please don't ask). This information needs to be accessed by several different classes, and displayed in view controllers on different tabs. A view controller will pull the information and make it available to all the other view controllers. A singleton provided an effective solution to this because only one data model of my physical object's state can exist at any given time, with the added benefit that I can access it from any view controller without much effort. I may be wrong, but I thought the purpose of a singleton was for models where there can be one and only one instance of your class? Singletons (like other design patterns) were designed to solve a specific problem. But I concede that they are probably overused.
- rbarooah 15y agoLetting individual classes get settings from NSUserDefaults (which itself effectively presents a singleton) directly seems to work pretty well for me. One advantage is that the classes are then reusable in other projects because they aren't coupled to an application specific settings provider.