Overridable Code Constants

last modified: November 30, 2005

OverridableCodeConstants is a configuration design pattern that attempts to blend the advantages of CODE_CONSTANTS with the flexibility of using values loaded from XML configuration files.

It's both traditional and convenient when coding to put literal values into code constants, because:

But its not convenient when you need to change these constants any more than infrequently, which is why configuration files, registry settings and the like are so universal, because commonly:

Static constants are typically assigned when a class is loaded to their value. The OverridableConstants pattern checks a backing configuration file at this time, and sets the constant to the value specified by the file, or to a default value otherwise. A Java example:

package model;
class ModelRunner {
  public static final int THREAD_COUNT = Config.getInstance().getInt("model.ModelRunner.THREAD_COUNT", 1);

What's nice is that:

Some implementation notes:

<config>
    <model>
        <ModelRunner>
            <THREAD_COUNT>5</THREAD_COUNT>
        </ModelRunner>
     </model>
</config>

Loading...