Binding and Bound Functions in Java

Printer QR Code ISO/IEC18004 in Java Binding and Bound Functions
Binding and Bound Functions
Print QR Code In Java
Using Barcode encoder for Java Control to generate, create QR-Code image in Java applications.
Now let s make a small change to the example shown in Figure 9-8 that will illustrate the difference between an unbound and a bound function we ll add the ability for the user to track either the area of the circle or its circumference as the radius variesTo make this possible, we add two radio buttons that allow the user to select which of these values should be displayed, as shown in Figure 9-95
Barcode Printer In Java
Using Barcode creation for Java Control to generate, create barcode image in Java applications.
This modified example can be found in javafxbinding/BoundFunctionfx in the JavaFX Book Desktop project
Read Barcode In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
9 Binding
Quick Response Code Creation In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create Quick Response Code image in .NET applications.
Figure 9-9
QR Code JIS X 0510 Creation In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
Allowing selection of the display of area or circumference
QR Generation In Visual Studio .NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
To implement this, we first add a function that computes the circumference given the radius:
Printing QR Code JIS X 0510 In VB.NET
Using Barcode maker for .NET framework Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
function computeCircumference(radius:Integer):Integer { Mathround(2 * MathPI * radius) as Integer; }
EAN 128 Printer In Java
Using Barcode maker for Java Control to generate, create GTIN - 128 image in Java applications.
To reflect the user s selection of area or circumference, we add a Boolean-valued script variable that indicates whether the user wants to see the area:
Creating Barcode In Java
Using Barcode generation for Java Control to generate, create barcode image in Java applications.
var showArea = true;
Bar Code Encoder In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
The state of this variable is bound to the selection state of the radio buttons shown at the top of Figure 9-9 but, because we haven t yet covered radio buttons, we won t show that code hereWe also add a function that returns a string containing either the area or the circumference of the circle, depending on the state of the showArea variable:
Generate Code 39 In Java
Using Barcode generator for Java Control to generate, create Code39 image in Java applications.
function getAreaOrCircumference(radius:Integer) { if (showArea) "Area: {computeArea(radius)}" else "Circumference: {computeCircumference(radius)}" }
Generating EAN13 In Java
Using Barcode creator for Java Control to generate, create EAN13 image in Java applications.
Finally, the value returned by this function is displayed in a Text object:
Create MSI Plessey In Java
Using Barcode maker for Java Control to generate, create MSI Plessey image in Java applications.
Text { content: bind getAreaOrCircumference(radius) x: 120 y: 80 }
Creating Bar Code In .NET
Using Barcode generation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
If you were to run the example with the code shown so far, you would see the user interface shown in Figure 9-10 If you were to move the slider to change the radius of the circle, the displayed area would be updated as it was in the previous example So far, so good If you were now to select the Circumference radio button, you would expect the circumference to appear in place of the area But, as you can see in Figure 9-10, that does not happen; the area is still displayed
Scanning ANSI/AIM Code 128 In VS .NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
Binding and Functions
Encoding Bar Code In Visual C#
Using Barcode drawer for VS .NET Control to generate, create barcode image in .NET applications.
Figure 9-10
ANSI/AIM Code 128 Printer In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create Code 128C image in .NET applications.
A case for using a bound function
Code 39 Full ASCII Encoder In Visual C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Code 39 Full ASCII image in Visual Studio .NET applications.
Selecting one of the radio buttons changes the value of the showArea variable, which is used in the body of the getAreaOrCircumference() function:
Bar Code Creation In VB.NET
Using Barcode drawer for .NET Control to generate, create bar code image in VS .NET applications.
function getAreaOrCircumference(radius:Integer) { if (showArea) "Area: {computeArea(radius)}" else "Circumference: {computeCircumference(radius)}" }
Code 39 Full ASCII Drawer In VS .NET
Using Barcode printer for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
This isn t enough to cause this function result to be reevaluated because the value of an unbound function depends only on its arguments in this case, only a change in the circle s radius will cause the function to be invoked againThere are two ways to fix thisThe first is to make the value of showArea one of the function argumentsThis would work, but in general this is not a good solution because in a more complex application you may end up having to pass arguments like showArea through multiple levels of function callsA better solution is to make getAreaOrCircumference() a bound function, which just requires the addition of the keyword bound:
bound function getAreaOrCircumference(radius:Integer) { if (showArea) "Area: {computeArea(radius)}" else "Circumference: {computeCircumference(radius)}" }
The value returned by a bound function is reevaluated when any of its dependencies changes that is, when its arguments or any of the values used in the function body, such as showArea in this example, are modifiedWith this change, toggling the selected radio button causes the displayed value to switch between area and circumference, as shown in Figure 9-11
Figure 9-11
Using a bound function
9 Binding
Optimization of Bound Function Evaluation
In the preceding section, we said that the value returned by a bound function is reevaluated when its inputs change we did not say that the function is invoked again to compute the return valueThe compiler is permitted to optimize the evaluation of an expression involving a bound function by arranging for only those parts of the function that directly depend on the dependency that has changed to be reevaluated In fact, it is frequently not necessary for the compiler to rerun the whole function For example, suppose we were to write a bound function called complex() that derives its result from the results of two other functions called slowFunction1() and slowFunction2():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 function slowFunction1(arg:Number):Number { println("slowFunction1"); Mathatan(arg) } function slowFunction2(arg:String):String { println("slowFunction2"); argtoUpperCase() } var inputString = "Input Value"; bound function complex(arg:Number):String { "The results are {slowFunction1(arg)}" " and {slowFunction2(inputString)}" }
In this artificial example, the bound function complex() depends both on its numeric argument and on the value of the variable inputString, binding it to a variable called result, like this:
var arg = 10; var result = bind complex(arg); println(result);
This produces the following output: