Contents

Class ValueHolder


Inheritance:

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.

Instance Variables

Instance Protocol

Note:

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.
      ]

However, this change is not always advantageous. For objects that have a time consuming equality check (defined in method =), the test can slow down your application.

Related stuff:

Class StringHolder is a container for a string that can be used as a model.

Example:

Follow this link for a worked example.


Contents