Search code examples
javajava-8observer-patternjava-9

Observer and Observable got deprecated and the reason stated is: they convey something has changed but not state what has changed, this is false?


Check this by open jdk

deprecate Observer and Observable

Observer and Observable Class:

Observable:-

class NewsAgency extends Observable {

String news;
public void setNews(String news) {
    this.news = news;
    setChanged();
    notifyObservers(news);
 }
}

Observers:-

public class IndiaNews implements Observer {
   @Override
   public void update(Observable o, Object news) {
   System.out.println("Indianews: " +news.toString());
 }
}

class AjjTak implements Observer {
@Override
public void update(Observable o, Object news) {
    System.out.println("AjjTak: " +news.toString());
}

Main CLass:-

public class Main {
public static void main(String[] args) {
    NewsAgency newsAgency = new NewsAgency();

    IndiaNews indiaNews = new IndiaNews();
    AjjTak ajjTak = new AjjTak();

    newsAgency.addObserver(indiaNews);
    newsAgency.addObserver(ajjTak);

    newsAgency.setNews("Yo");
}

}

But I was able to tell the observer that news has changed then How that above statement is true?

Can anybody clear my thoughts?

Thanks :)


Solution

  • But I was able to tell the observer that news has changed then. How that above statement is true?

    The text from the issue tracker link says:

    "For example, they support only the notion that something has changed, but they don't convey any information about what has changed."

    In your example, the update method call tells the observer that something has changed, but not what actually changed. So, when you call setNews("Yo"), the indiaNews observer is told that newsAgency has changed. But it is NOT told what the change was. The observer can use the news argument to see the current state of the newsAgency, but there is no way to see what the state was before the change, or indeed what it was immediately after the change1.

    In many use cases for Observer / Observable, the application needs to know what actually changed to trigger the update call.

    THAT is the deficiency that being highlighted, and it is one of the reasons for the deprecation. The deprecation text in the javadocs list others as well:

    "This class and the Observer interface have been deprecated. The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrent package. For reactive streams style programming, see the Flow API."


    1 - Since the order of notification delivery is unspecified, the state of the observed object could have changed again by the time that the update call occurs.