1

I am writing an application where a user will have a custom config json file. The json is just a dictionary where they define new objects of the exact same type. Here is an example with outfits:

'summer'   : {'footwear' : 'flip-flops',
              'bottom'   : 'shorts',
              'top'      : 't-shirt'},
'snow'     : {'footwear' : 'snow boots',
              'bottom'   : 'jeans',
              'top'      : 'thermal shirt',
              'overwear' : 'coat'}
'academic' : {'footwear' : 'leather shoes',
              'bottom'   : 'slacks',
              'top'      : 'gown',
              'headwear' : 'cap'}

I know from experience that I want only one class to interact with this config file, and there should only be one of them, so the class I want to make should be a singleton which also creates objects for other classes (kind of factory-esque), and handles the validation and writing of new data to the file.

What would you name this kind of class? This seems like a common enough problem that better programmers should have some pretty good solutions to.

1
  • This seems like a common enough problem surprisingly, it's not when it comes to config files which often are read-only and not updatable in runtime because they are loaded during the bootstrap, so the whole application is running and configured with the initial values. Hot-swap configurations are not that easy to implement because you have to consider the impact on ongoing tasks. This subject is particularly hard in distributed systems too. The pattern or solution will depend (a lot) on this sort of thing. Once more, no silver bullets here.
    – Laiv
    Oct 31 at 9:15

1 Answer 1

3

Do not confuse the need for only one object with the need to utilize the Singleton Design Pattern. Your application needs only one object that writes to the file at the same time. You can accomplish this by passing a reference to the same object to anything that needs to operate on this data. Utilize Dependency Injection and Inversion of Control rather than using a Singleton. Many times, the Singleton Design Pattern is just a fancy way of saying "global variable" which includes all the design baggage that goes along with global state.

Generally we refer to any object that accesses or modifies persistent data as a Data Access Object. There is no single way of doing this. Search for data access patterns for some common ways of implementing this kind of behavior.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.