Java Reference
In-Depth Information
controller.incrementIPropertyOnModel();
controller.changeStrPropertyOnModel();
controller.switchColorPropertyOnModel();
}
}
When we run the program in Listings 4-9 to 4-12, the following output is printed to the console:
Property i changed: old value = 0, new value = 1
Property str changed: old value = Hello, new value = World
Property color changed: old value = 0x000000ff, new value = 0xffffffff
Property i changed: old value = 1, new value = 2
Property str changed: old value = World, new value = Hello
Property color changed: old value = 0xffffffff, new value = 0x000000ff
Understanding the Lazily Instantiated Properties Strategy
If your JavaFX Bean has many properties, instantiating all the properties objects up front at bean creation time may
be too heavy an approach. The memory for all the properties objects is truly wasted if only a few of the properties are
actually used. In such situations, you can use one of several lazily instantiated properties strategies. Two typical such
strategies are the half-lazy instantiation strategy and the full-lazy instantiation strategy.
In the half-lazy strategy, the property object is instantiated only if the setter is called with a value that is different
from the default value, or if the property getter is called. The program in Listing 4-13 illustrates how this strategy is
implemented.
Listing 4-13. JavaFXBeanModelHalfLazyExample.java
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class JavaFXBeanModelHalfLazyExample {
private static final String DEFAULT_STR = "Hello";
private StringProperty str;
public final String getStr() {
if (str != null) {
return str.get();
} else {
return DEFAULT_STR;
}
}
public final void setStr(String str) {
if ((this.str != null) || !(str.equals(DEFAULT_STR))) {
strProperty().set(str);
}
}
 
Search WWH ::




Custom Search