Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A general pattern type question  RSS feed

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a class that controls the GUI and this triggers a bunch of methods in another class which actually does the work - where would you control updates to the GUI depending on the work done? One way of doing this is to pass the JLabel object (for example) to the work class and update it there but there must be a better standard way of doing this.
The components are, in effect accessed from two different classes so should there be a separate singleton class for the components??
Thanks.
Paul
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
view classes, controlling classes and data classes should be encaplsulated and seperated from each other. sure, they have to talk to each other, but this should be done in only one place. If you have to change the way they communicate you will do that in this one place and nowhere else. If you share the JLabel objects in two different classes you have to change both of them if you decide to switch from a JLabel to a JTexField or a JTable (which will cause even more changes). Thus - concentrate the view inside a single class (or parts of the whole view inside one class). Decide what values to display in the view just by transmitting the values directly (as Strings or as little messenger objects that hold only data - like a Person object e.g.).
In fact, you will end up with some controlling logic in the view. But I could not think of another way without giving away the references on components. and I would omit that, in the first place.
For example: I have state objects that describe GUI states but in terms of actions that shall be possible to the user (like - searchRunning(), searchPossible() etc.) Each view has a method switchState(State state)). In this method the view checks for flags that are important to this special view. The view displaying the search button will check for searchPossible() - if this returns true, it will set the button enabled, otherwise disabled.
Chantal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!