In this short video I’d like to discuss properties, we will start with a plain old Java object or POJO for short.
You probably wrote a lot of these object classes and understand it at a glance. We use getters and setters for encapsulation. Those are the most basic properties.
A more modern version of this is available in the Codename One API. At first glance this seems like we just defined public fields, but since they are final they can’t be modified. Every property has a name and type associated with it, the property index keeps track of all the properties and as we go thru the rest of the video you will understand why.
But first lets look at how this should work. The POJO can be used with standard getters and setters as we see above. Below you can see the property usage. Notice that we still have a setter and getter but the syntax is different. It’s more generic
We can also construct an object by chaining setters together. This removes the need to create many obtuse constructors for an object
Encapsulation can still be kept, we can override get and set just like any other getter and setter so we can have the full flexibility of the POJO. We also get a lot of free stuff in the bundle, for instance every property is observable which means we can bind a listener to changes in the property value and write logic to handle that.
Implementing common object methods like toString, equals and hashCode is very easy with properties as the index keeps track of the properties and allows us to introspect the object. Introspection is the process of discovering the values of the properties in runtime
But lets go to the cool stuff… We can instantly parse JSON or XML directly into a property object and mapping works seamlessly. We can also generate JSON or XML instantly with one line of code. Again this is powered by introspection which is now possible and works even with obfuscated code
Features such as serialization or externalization become trivial
But the really cool functionality is simple database mapping. ORM stands for Object Relational Mapping. We didn’t implement anything nearly as elaborate as JPA which is probably an overkill for mobile devices and SQLite. We created a simple CRUD API that allows you to insert, update, delete and select from SQL. You can also create your tables automatically and define primary key behaviors.
Another great capability is the ability to bind an object property to a component. Changes made to the text field will implicitly update the property and visa versa. This can apply to many component & property types from pickers to check boxes onwards.
But the real cool stuff is the instant UI. It can automatically generate a UI from the property object
This UI was generated by instant UI for the meeting object, it includes many hidden features such as using numeric constraints in the right place and implicit support for table layout.
But I can go on about this for an hour, it’s time to finish. Thanks for watching and I hope you found this helpful