• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Method Chaining Execution and Evaluation  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I am learning Java programming from scratch i found the lines below in some of my coding lessons. I understand what they do as a whole but i dont understand why they need to be that way and how are they exexuted.


;



Why does the Second Line requires GetSelectionModel ? and just doesnt use SetSelectionMode ?



Example below is about adding a change listener to our TodoList options so when something is clicked on it, its details shown in our 2 boxes.



Why doesnt first line just can't be :  todoListView.addListener(new ChangeListener()) ?

I tried looking up Java Method Chaining in google but none of them show for things like i wrote. If you can help me out that would be great.
 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
todoListView is presumably of the type ListView. A ListView doesn't have a setSelectionMode() method; that's a method of MultipleSelectionModel. It also doesn't have a selectFirst() method; that too is a method of MultipleSelectionModel.

So, in both cases we need to get the selection model of the list view in order to set the selection mode and the selection: Those are both properties of the selection model, and not of the list view.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
John Doherty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:todoListView is presumably of the type ListView. A ListView doesn't have a setSelectionMode() method; that's a method of MultipleSelectionModel. It also doesn't have a selectFirst() method; that too is a method of MultipleSelectionModel.

So, in both cases we need to get the selection model of the list view in order to set the selection mode and the selection: Those are both properties of the selection model, and not of the list view.



Campbell Ritchie wrote:...and welcome to the Ranch



Thank you both. It makes a lot more sense now. I will try to look at other cases where there are more method chains and see if they work the same way.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like
frame.getContentPane().add(...);
where frame is an instance of JFrame. Also find the add() method in the same class, which has been added to avoid the content pane call.

Also search for “Law of Demeter”. I think it is a bit of exaggeration to call that a Law, but it is worth reading about.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope Campbell's reference to the Law of Demeter was made to suggest that the kind of method chaining you have in those examples is not good practice. It breaks the todoListView's encapsulation and externalizes logic/operations that would be best kept internal to that object.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit on the fence regarding the law of Demeter.

For an already complex type like ListView, would you really add an extra convenience method for every piece of behavior of every property that it exposes? Just take a look at the properties:
  • focusModel
  • items
  • orientation
  • placeholder
  • selectionModel


  • Each of them contains many more methods. Should they all be flattened into ListView?
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I agree with both of you: yes, such method chaining does expose implementation details, and also that convenience methods take a long time to write and make the public interface of the class so much larger.
    I am also not sure whether I agree completely with the “Law” of Demeter.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:
    For an already complex type like ListView, would you really add an extra convenience method for every piece of behavior of every property that it exposes? Just take a look at the properties:
    ...
    Each of them contains many more methods. Should they all be flattened into ListView?

    That's not the only way. In fact, that's not even a way I'd consider as adhering to Demeter.

    Instead, I'd ask "What's the intent of todoListView.getItems().setAll(todoItems)?" Is it not just to add new ToDo items? Then why not just have a method called "addTodoItems()" in the class that manages the todoListView ?
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!