Object » Model » ValueHolder
A ValueHolder is a Model that contains a value.
A ValueHolder is a suitable model for a passive view, that is one that displays a value but does not process input. For input-processing views more functionality is needed.
It is helpful to describe a ValueHolder as an 'intelligent variable', that is one that informs its subscribers (=observers) about a change of its value. A ValueHolder can have any number of observers and is therefore useful to share a piece of information between several objects. A ValueHolder is a shared variable of its observers.
A ValueHolder stores a state information and exhibits observable change of its state. With these properties it is an elementary brick for the construction of structured state engines.
Being a container for a value, a ValueHolder has the properties of a variable. Instances that reference the same value holder have in fact a shared variable.
dependents - DependentsArray
The collection of the dependents of the ValueHolder. The dependents are
the receivers of update-messages that are sent when a change is broadcasted.
contents - Object
The piece of information that the value holder stores.
contents
Answers the current value of the instance variable contents.
contents: newContents
Assigns newContents to the instance variable contents and
sends update: #contents to all dependent objects.
The dependent objects are always notified, even when the newContents does not differ from the previously stored value. It is possible to add a simple check in method contents: that avoids unnecessary notifications:
contents: newContents contents ~= newContents ifTrue: [contents := newContents. self contentsChanged. ]
Class StringHolder is a container for a string that can be used as a model.
Follow this link for a worked example.