Keep the configuration out of the codePosted on Oct 27, 2021, three minutes to read.
Sometimes, it’s not a complex design pattern but a small idea you borrow from a different language that will get the job done.
On one project, I had a factory method where an appropriate class is used based on a configuration:
Over time this block of code grew substantially. The amount of possible values for setting1 skyrocketed. Also, other settings came into play.
My initial idea was to use a map of possible Style classes, pick one based on a
setting1 value and go from there as I would in Python:
This approach turned out tricky to pull off in C++. Nevertheless, it brought me an idea. After an investigation, I found out the Style classes are internally effectively JSONs - that is, they only contain a bunch of properties we read one way or another. Instead of selecting an appropriate class, I can pick a JSON file, load the values from the JSON instead of a Style class and process that the way I do now.
With that in mind, it was sufficient to replace all the Style classes with a JSON object. I store the JSON in resources for all configurations. It is then enough to extract maps and lists from the JSON and directly process those. As a result, we no longer need the inheritance nor mangroves of conditions.