More C++ Idioms/Algebraic Hierarchy

= Algebraic Hierarchy =

Intent
To hide multiple closely related algebraic abstractions (numbers) behind a single generic abstraction and provide a generic interface to it.

Also Known As

 * State (Gamma et al.)

Motivation
In pure object-oriented languages like Smalltalk, variables are run-time bindings to objects that act like labels. Binding a variable to an object is like sticking a label on it. Assignment in these languages is analogous to peeling a label off one object and putting it on another. On the other hand, in C and C++, variables are synonyms for addresses or offsets instead of being labels for objects. Assignment does not mean re-labelling, it means overwriting old contents with new one. Algebraic Hierarchy idiom uses delegated polymorphism to simulate weak variable to object binding in C++. Algebraic Hierarchy uses the Envelope Letter idiom in its implementation. The motivation behind this idiom is to be able to write code like this:

Solution and Sample Code
Complete code showing an implementation of the Algebraic Hierarchy idiom:

Related Idioms

 * Handle Body
 * Envelope Letter