Java Reference
In-Depth Information
The creation operators provide a convenient way of creating a binding without directly extending the abstract
base class. It takes a Callable and any number of dependencies as an argument. The area double binding in
Listing 4-3 can be rewritten as follows, using a lambda expression as the Callable :
DoubleBinding area = Bindings.createDoubleBinding(() -> {
return x.get() * y.get();
}, x, y);
The selection operators operate on what are called JavaFX beans , Java classes constructed according to the
JavaFX Beans specification. We talk about JavaFX Beans in the “Understanding JavaFX Beans Convention” section
later in this chapter.
There are a number of methods in Bindings that deal with observable collections. We cover them in Chapter 7.
That covers all methods in Bindings that return a binding object. There are 18 methods in Bindings that do not
return a binding object. The various bindBidirectional() and unbindBidirectional() methods create bidirectional
bindings. As a matter of fact, the bindBidirectional() and unbindBidirectional() methods in the various
properties classes simply call the corresponding ones in the Bindings class. The bindContent() and unbindContent()
methods bind an ordinary collection to an observable collection. The convert() , concat() , and a pair of overloaded
format() methods return StringExpression objects. And finally the when() method returns a When object.
The When and the StringExpression classes are part of the fluent interface API for creating bindings, which we
cover in the next subsection.
Understanding the Fluent Interface API
If you asked the questions “Why would anybody name a method when() ? And what kind of information would
the When class encapsulate?,” welcome to the club. While you were not looking, the object-oriented programming
community invented a brand new method of API design that totally disregards the decades-old principles of object-
oriented practices. Instead of encapsulating data and distributing business logic into relevant domain objects, this
new methodology produces a style of API that encourages method chaining and uses the return type of one method
to determine what methods are available for the next car of the choo-choo train. Method names are chosen not to
convey complete meaning, but to make the entire method chain read like a fluent sentence. This style of APIs is called
fluent interface APIs .
You can find a more thorough exposition of fluent interfaces on Martin Fowler's web site, referenced at the end
of this chapter.
Note
The fluent interface APIs for creating bindings are defined in the IntegerExpression series of classes.
IntegerExpression is a superclass of both IntegerProperty and IntegerBinding , making the methods of
IntegerExpression also available in the IntegerProperty and IntegerBinding classes. The four numeric expression
classes share a common superinterface NumberExpression , where all the methods are defined. The type-specific
expression classes override some of the methods that yield a NumberBinding to return a more appropriate type of binding.
The methods thus made available for the seven kinds of properties and bindings are listed here:
BooleanProperty and BooleanBinding
For
BooleanBinding and(ObservableBooleanValue b)
BooleanBinding or(ObservableBooleanValue b)
BooleanBinding not()
BooleanBinding isEqualTo(ObservableBooleanValue b)
 
 
Search WWH ::




Custom Search