The Context Tree Design Pattern is broadly used in framework design and fascinatingly appears in seemingly unrelated applications implemented with man

The Context Tree Design Pattern

submited by
Style Pass
2021-05-23 18:30:07

The Context Tree Design Pattern is broadly used in framework design and fascinatingly appears in seemingly unrelated applications implemented with many different strategies. As far as I can tell, this pattern does not yet have a name and so I made one up. C2 documents the Context Object and why it is bad, yet falls short of identifying the relationship to trees that push the pattern beyond a fancy bag of global variables.

The Context Tree Design Pattern appears whenever there is some kind of tree (in the computer science sense) and ancestor nodes want to communicate state to all their transitive children. Such trees might be:

One common capability of setting context in a tree is the ability to shadow named context values that were specified by an ancestor with a different value for all children.

This method leads to significant boilerplate and leaks the existence of context to every participant in the call tree. However, sometimes, such as when crossing system boundaries, this may be the only implementation option. Frameworks can hide this from user code through mechanisms that pass context via standardized headers or side channels for remote procedure calls.

Leave a Comment