To support its simple, declarative programming model, compiled JavaFX Script has some constructs to eliminate the need for setter and constructor methods. Please take a look at the following example and its console output, and I'll follow up with an explanation of two of these constructs.
/*
* TriggersAndPostinit.fx - Example of using a replace trigger
* to eliminate the need for a setter,
* and a postinit block for some
* constructor-related functionality
*
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/
import java.lang.System;
class Vehicle {
postinit {
if (numWheels == 18 and sporty) {
System.out.println("Veto sporty for 18 wheeler!");
sporty = false;
}
}
attribute model:String;
attribute numWheels:Integer = 4
on replace (oldValue) {
if (numWheels <= 0) {
System.out.println("{numWheels} wheels not valid, changing to {oldValue}!");
numWheels = oldValue;
}
}
attribute sporty:Boolean;
public function toString():String {
return "Model: {model}, Number of wheels: {numWheels}, Sporty: {sporty}"
}
}
var corvette =
Vehicle {
model: "Corvette"
numWheels: 4
sporty: true
};
System.out.println("{corvette}
");
corvette.numWheels = 0;
System.out.println("{corvette}
");
var mackTruck =
Vehicle {
model: "Mack truck"
numWheels: 18
sporty: true
};
System.out.println("{mackTruck}
");
Please see previous posts like this one for how to build and run compiled JavaFX Script programs. Here's the console output when running this program:
Model: Corvette, Number of wheels: 4, Sporty: true
0 wheels not valid, changing to 4!
Model: Corvette, Number of wheels: 4, Sporty: true
Veto sporty for 18 wheeler!
Model: Mack truck, Number of wheels: 18, Sporty: false
Eliminating Setter (Mutator) Methods
In this example, a class named Vehicle is defined, from which we're going to create instances. One very important role of a setter method is to protect the integrity of the object. In this example, we don't want the number of wheels on a vehicle to be set to 0 or less. Traditionally, a setNumWheels() method would be created in which an invalid value would be rejected. To support the declarative model, compiled JavaFX Script has an on replace trigger that gets invoked whenever the value of an attribute is replaced. In the example above, note that the on replace block rejects the new value and sets the attribute value back to the former value. Note that oldValue is an arbitrarily named variable that is local to the on replace block.
Providing Constructor-Related Functionality
After after a new object has been created, and all of its attributes have been assigned their default or explicitly set values, the postinit block (if present) is executed. This gives you the opportunity to perform functionality, including protecting the integrity of the object, when all of the attribute values are known. In the example above, an 18-wheeler truck can't be sporty as well (although I've seen some pretty sporty semi-trucks on the road).
Please also note the toString() function in this example. When you use an object reference in a String, the toString() function is automatically called. Here, I've overridden the default toString() function to make it easy to print its contents in the desired format.
Oh, by the way, I'm hearing more voices. Check out this post on Maxa Blog.
As always, if you have any questions, please post a comment.
Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site
i like this part of the blog:"Please also note the toString() function in this example. When you use an object reference in a String, the toString() function is automatically called. Here, I've overridden the default toString() function to make it easy to print its contents in the desired format." is very good
Posted by: propecia | April 26, 2010 at 11:36 AM
this is the link: http://www.javamexico.org/blogs/rodrigo_salado_anaya/no_need_setters_and_constructors_compiled_javafx_script
Thanks.. =)
Posted by: rodrigo salado anaya | April 22, 2010 at 12:28 PM
I transcribed the example (1.2 version) in www.javamexico.org, if they have a problem, I will remove it immediately. thanks :)
Posted by: rodrigo salado anaya | April 22, 2010 at 11:44 AM
"thanks I was looking for such a article, but how can you create custom getter is a way c# or flex does it"
kamiseq,
You can create functions such as getVariableName() if needed. setVariableName() is unnecessary because of "on replace" triggers. To the extent that you can, it is best to keep the state of a public variable updated so that the getVeriableName() is not required. I agree that sometimes this is not practical.
Thanks,
Jim Weaver
Posted by: James Weaver | March 04, 2009 at 08:31 AM