Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Replacing deprecated Observer/Observable with an interface

 
Ranch Hand
Posts: 37
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Long story short: the  upgrade of my computer has meant the swap from Oracle JDK to OpenJDk version 15 and Observer/Observable have been declared deprecated!
In the following code (inspired from the last version found in this thread) I just want the rows of the table to be updated.

So, I replace Observer/Observable with PropertyChangeListener/PropertyChangeSupport. It runs as expected.

By curiosity I would like to know how to do it with the use of an interface. Any hint?

On a side note, in class MyRow, m_rank tracks the rank of the row in the ArrayList to avoid looking for it in a loop (see the previous code above). Is there a straightforward way to do it without the use of the explicit member m_rank?

Thank you for your help,
Greg


 
Saloon Keeper
Posts: 12825
279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Pata wrote:By curiosity I would like to know how to do it with the use of an interface.


I'm not quite sure what you're asking here. An interface for what?

On a side note, in class MyRow, m_rank tracks the rank of the row in the ArrayList to avoid looking for it in a loop (see the previous code above). Is there a straightforward way to do it without the use of the explicit member m_rank?


It's fine, except I would call it index instead of rank, since the purpose of a variable named index is more easily recognized.

The real problem is with your updateRanking() method. If you instead make sure to keep the index of a row updated as soon as you insert or remove a row, you don't need a method that updates ALL rows. For example, if you add a removeRow() operation to your table model, it can update the indices of just the rows that came after the removed row.

Some other comments about your code:

  • Make your classes final, unless you carefully designed them to be extensible. This is not a trivial task.
  • Don't use global variables such as INSTANCE. Static variables must only be used for constants and for cross-cutting concerns such as loggers.
  • Make all your fields private always.
  • Don't start your field names with pointless prefixes such as "m_". It's just another variant of Hungarian notation that clutters your code and doesn't add any semantic meaning.
  • Write out your variable names. "m_t" doesn't mean anything and doesn't say anything about its purpose. Write "feedTimer".
  • Always run all UI code on the event dispatch thread, even if you are just initializing your GUI before it is first displayed. Use SwingUtilities.invokeLater() in your main method.
  • You can use lambda expressions to define simple event listeners.
  • There is no point in extending classes if you're not going to add behavior. Just use JTable instead of MyTable.
  • Don't let your UI components implement listeners. Use lambda expressions or method handles for simple listeners, and dedicated listener classes for complex listeners, like you did with TableActionListener.
  • Members that are not inherited should not have a higher visibility than the class they are in. See MyModel.getRows().
  • Don't return mutable internal state from your methods. A rogue class can break your MyModel class by getting the m_rows list and modifying it directly.
  • Don't use System.out to trace program execution. Use a logger.
  • Don't return null to indicate an invalid method argument. The default case in getColumnName() should throw an IndexOutOfBoundsException.
  • MyRow.getSize() implies it gets the size of a row. It doesn't. It gets the value of one of the row's cells. Name it getCellValue().
  • Declare constants instead of using magic values, such as B_COLUMN_INDEX and SIZE_PROPERTY_NAME.
  • Why did you name a simple counter field salt? Just name it count or i.
  •  
    Greg Pata
    Ranch Hand
    Posts: 37
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Stephan for your time.

    You are right. I will improve the code as you suggest.

    The main question is: how to use an interface to replace the deprecated Observer/Observable or propertyChangeSupport/PropertyChangeListener?
     
    Saloon Keeper
    Posts: 6896
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why an interface?

    If you want a generalized event distribution system, check out event buses like Green Robot.
     
    Greg Pata
    Ranch Hand
    Posts: 37
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Tim for your suggestion.

    Tim Moores wrote:Why an interface?


    This is what is suggested here by Stephan.
    It could be simple in my test code but I don't know how to do it.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12825
    279
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Note that I wrote that post before Java 9 came out and deprecated Observer in favor of PropertyChangeListener.

    You can still follow my advice in that topic if you don't like how generic PropertyChangeListener is, and would rather use a listener interface that is more tightly coupled with your problem domain. For instance, you could write an interface like this:

    MyRow could contain the following declarations:

    And you can call addRowChangeListener() in MyModel.addRow():
     
    Greg Pata
    Ranch Hand
    Posts: 37
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much Stephan! Brilliant!
    I appreciate. It is not as simple as I thought.
     
    Greg Pata
    Ranch Hand
    Posts: 37
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The corrected code follows. The interface version will appear as soon as PropertyChangeSupport becomes deprecated. :-)
     
    This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic