Java Reference
In-Depth Information
public StringProperty strProperty() {
if (str == null) {
str = new SimpleStringProperty(this, "str", DEFAULT_STR);
}
return str;
}
}
In this strategy, the client code can call the getter many times without the property object being instantiated. If
the property object is null, the getter simply returns the default value. As soon as the setter is called with a value that
is different from the default value, it will call the property getter, which lazily instantiates the property object. The
property object is also instantiated if the client code calls the property getter directly.
In the full-lazy strategy, the property object is instantiated only if the property getter is called. The getter and
setter go through the property object only if it is already instantiated. Otherwise they go through a separate field.
The program in Listing 4-14 shows an example of a full-lazy property.
Listing 4-14. JavaFXBeanModelFullLazyExample.java
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class JavaFXBeanModelFullLazyExample {
private static final String DEFAULT_STR = "Hello";
private StringProperty str;
private String _str = DEFAULT_STR;
public final String getStr() {
if (str != null) {
return str.get();
} else {
return _str;
}
}
public final void setStr(String str) {
if (this.str != null) {
this.str.set(str);
} else {
_str = str;
}
}
public StringProperty strProperty() {
if (str == null) {
str = new SimpleStringProperty(this, "str", DEFAULT_STR);
}
return str;
}
}
Search WWH ::




Custom Search