Object-oriented – Pattern(s) about hierarchical settings overwriting

architectural-patternsdesign-patternsobject-orientedpatterns-and-practices

Assume that you have a hierarchy of organizational units:

- Company
-- Branches
--- Departments
---- Teams

Lets say I have some settings (for simplicity assume that they have the same properties) for each level of hierarchy:

  • Max nr of documents
  • Min something else
  • Can do X
  • Can do Y
  • many other properties

So, the sub-units can reuse as default the parents settings, or they have only one or two of these settings modified. Same for their sub-units.

What would be the proper pattern to follow – both as code structures, as well as db storage to support that, with the idea that each unit's settings can be modified independently, and sub-unit's settings override the default of the parent.

We were thinking about key/value pairs, and then lookup for each one going up the hierarchy until we find the right setting, but this can become ugly pretty fast.

Another option we were considering is that each unit has it's own record of the settings, and update accordingly when parent's settings changed.

I'd appreciate any suggestion of well known approaches/patterns in that regard.

Best Answer

The design I follow is to only set what's overridden. So I would have a full set of default settings. Then at the company level I would override only those that are different. At the branch level I would only override those that are different than the company or default. And so on. In that way the settings build up from the base. Changing a lower level setting will "bubble up". This also keeps your data set as small as possible.

At the application level, assuming the set is relatively small, I would load all of the defaults, then the relevant company's overrides, then the relevant department's overrides, etc. into one data set. You can then look up any setting from that one list.

Related Topic