Which design patterns can be applied to the configuration settings problem

application-settingsconfigurationconfiguration-filesdesign-patterns

In large and complex software products managing configurable settings becomes a major pain. Two approaches I've seen to the problem are:

  • have each component in the system load its own configuration from config files or registry settings.
  • have a settings loader class that loads all the configurable system settings and have each component query the settings loader for its settings.

These approaches both feel wrong to me.

Are there any design patterns that could be used to simplify the problem? Maybe something that would take advantage of the dependency injection technique.

Best Answer

I prefer to create an interface for setting query, loading, and saving. By using dependency injection, I can inject this into each component that requires it.

This allows flexibility in terms of replacing the configuration strategy, and gives a common base for everything to work from. I prefer this to a single, global "settings loader" (your option 2), especially since I can override the configuration mechanism for a single component if I absolutely need to do so.