If a function body is described with the keyword once instead of do , then that function is shared by all instances of the class and its defining body is only executed once. Ensuing calls return a copy of the reference it returned the first time. Only the first usage of either constant creates it. This next example shows how a once function can be used to ensure that all the commands in a menu system can be guaranteed to refer to the same subject. Using other methods described earlier, it was only possible to ensure that the options had a reference to a model, not necessarily the same one.
Global variables are unnecessary.
What is required is a mechanism for several runtime objects to have access to a shared object. A shared object should retain control over how it is created and what operations are performed upon it, regardless of the purposes for which it is being shared. Temporal coupling should be minimised or eliminated. A group of client modules should not be dependant upon a particular client to initialise a shared object. Nor should subsequent calling sequences endanger the consistency of a shared object.