I have several classes in my project that rely on configuration files in the same format. All of them are created once only during setup. They are not related, some of them represent simple data structures, others are more complex.
It feels redundant to copy basic file reading/xml parsing across all these classes' constructors, and I want to manage my IO centrally e.g. I don't want client code be able to modify configuration files.
Any cool design patterns that deal with this?
Name:
Anonymous2010-12-28 9:03
Just make a separate class(/classes) that handles IO.
Very simple, very easy.
so I make a class that opens the configuration files, and a structure that contains configuration settings that can be passed to my classes?
Name:
Anonymous2010-12-28 9:59
Perhaps consider a module (or static/singleton class depending on the language) specifically for loading and holding the config info.
It would have a setup/load method that you call on startup that does the parsing and stores the loaded information to be accessed elsewhere.
In Python 2.7, a new means of configuring logging has been introduced, using dictionaries to hold configuration information. This provides a superset of the functionality of the config-file-based approach outlined above, and is the recommended configuration method for new applications and deployments. Because a Python dictionary is used to hold configuration information, and since you can populate that dictionary using different means, you have more options for configuration. For example, you can use a configuration file in JSON format, or, if you have access to YAML processing functionality, a file in YAML format, to populate the configuration dictionary. Or, of course, you can construct the dictionary in Python code, receive it in pickled form over a socket, or use whatever approach makes sense for your application.
Name:
Anonymous2010-12-28 19:18
You're looking for an IoC framework like Spring, or whatever the equivalent is in your chosen language.