Java's Getters And Setters are evil, JavaFX Script's not always

The reaction for the posts Getters and setters: an antipattern. JavaBeans and OO and Summary: Public getters and setters should be considered as an antipattern caused some interesting discussions and comments. There was one common sense: exposing private attributes over getters and setters is neither objectoriented, nor domain driven style. An interesting solution for this problem offers JavaFX script, sample:


A class customer with 2 members and one update method:


import java.lang.System;

class Customer{

private attribute firstName: String?;

private attribute lastName: String?;

operation update(newFirstName:String,newLastName:String);

}

 

operation Customer.update(newFirstName:String,newLastName:String){

firstName = newFirstName;

lastName = newLastName;

}


The interesting part of the source code are "Replace Trigger" which can be associated with attributes: 


trigger on Customer.firstName[oldValue] = newValue{

System.out.println("Old value was: {oldValue}, the new value is: {newValue}");

}

 

trigger on Customer.lastName[oldValue] = newValue{

System.out.println("Old value was: {oldValue}, the new value is: {newValue}");

}

 

The construction of an FX object:

 

var dukeCustomer = Customer{

firstName: "duke"

lastName: "mighty"

};



Causes the following output:


Old value was: , the new value is: duke
Old value was: , the new value is: mighty


The call of the following update method: 


dukeCustomer.update('java','javafx');


writes the lines below:


Old value was: duke, the new value is: java
Old value was: mighty, the new value is: javafx



So JavaFX Script allows, only if neccessary, the definition of "interceptors" called triggers, which can be used for validation etc.

I like the idea, because most of the Java-Getters/Setters are empty. I'm curious, why these triggers aren't called "update aspects".

It would be much better for the marketing :-).

The method update isn't necessary, it is also allowed to access the attributes directly. It seem's like JavaFX now ignores the "private" keyword...


Hint: to see the system output from the JavaFXPad, you have to activate the "Java Console" in the JNLP settings (System Settings, Java under Windows).


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

The getters and setters debate has been raging for awhile now. Not sure where I stand but interesting twise with the JavaFX factor.

Posted by JavaDonkey on June 01, 2007 at 06:52 PM CEST #

Sort of unrelated but I'm also impressed by the integration of database concepts... It's clear the design team around F3/JavaFX (Christopher only?) are well-rounded, in-the-trenches type of people.

Posted by Ivan on June 01, 2007 at 07:12 PM CEST #

I just came here because I read your J1 presentation where you discuss binding JPA objects to UI using 295... I was very surprised to see something like that, have you tried this?
295 requires property change listeners in order to build proper two way binding, that doesn't seem like a good idea to add into a JPA object (PropertyChangeSupport objects). Yet I didn't see any mention of that in the presentation.

Anyway, regarding getters and setters have you seen my bean properties project?

It pretty much addresses most of the issues and I think solves them more elegantly than F3 (arguably the syntax could be improved).

Posted by Shai Almog on June 03, 2007 at 12:21 AM CEST #

Hi Shai,

the problem with J1-presentation is the 1 hour limit :-). I used Eclipse's Beans Binding, my own jakarta commons beanutils based solution and I'm also experimenting with Beans Binding. PropertyChangeSupport stuff is not perfect, but can be easily generated (see Netbeans 6 -> Java Desktop Application). A two-way binding is only needed in case there are more than one view interested in one domain projects. The JavaFX solution surprised me - it is interesting.
I'm already curious about your solution!

thank you for your comment,

adam

Posted by Adam Bien on June 03, 2007 at 11:50 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License