ValueCell Subclass 1
ValueCell
is the base class implemented by all cells. This class defines the
cell interface which consists of the following methods:
-
Adds an observer that is notified when the value of the cell changes.
-
Removes an observer, previously added with
addObserver
, so that it is no longer notified when the value of the cell changes.
and the following property:
-
The value of the cell, which can either be retrieved from memory or computed on demand.
To implement your own ValueCell
subclass, you have to implement
these methods and properties. For example to implement a cell that
computes a random value, whenever its value is accessed, the value
property has to be overridden:
class RandomCell extends ValueCell<int> {
final int max;
const RandomCell([this.max = 10]);
int get value => Random().nextInt(max);
void addObserver(CellObserver observer) {}
void removeObserver(CellObserver observer) {}
}
The value
property is overridden with a property that returns a
random integer. Note, a new random integer is returned whenever the
value of the cell is accessed.
The addObserver
and removeObserver
methods are overridden, since
these methods are not defined by the base ValueCell
class, with
empty methods. These methods are left empty since the cell never
notifies its observers that its value has changed and thus there is no
need to track the observers.
Custom Cells with Arguments
To define a ValueCell
subclass with a value that is dependent on
other cells, extend the
DependentCell
class. This class provides a constructor which accepts the set of
argument cells on which the value of the cell depends. Whenever the
value of at least one of the cells in this set changes, the observers
of the DependentCell
are informed. Only the value
property has to
be overridden by a subclasses, since DependentCell
provides
implementations of addObserver
and removeObserver
.
Here's an example of the previous RandomCell
however with the
maximum now provided in an argument cell rather than a value.
class RandomCell extends DependentCell<int> {
final ValueCell<int> max;
const RandomCell(this.max) : super({max});
int get value => Random().nextInt(max.value);
}
Some points to note from this example:
max
is now aValueCell
which is included in the argument cell set provided to thesuper
constructor.- The
value
property is accessed directly sinceDependentCell
does not determine argument cells dynamically.
DependentCell
is a stateless cell that does not cache its
value. Instead it's value is recomputed whenever it is accessed.