Skip to content

Instantly share code, notes, and snippets.

@mtorchiano
Created April 23, 2018 11:33
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mtorchiano/e69ac7e309fee81bd17f4f0740b9ffa9 to your computer and use it in GitHub Desktop.
Save mtorchiano/e69ac7e309fee81bd17f4f0740b9ffa9 to your computer and use it in GitHub Desktop.
Replacing Observer-Observable (inheritance vs. composition)

Replacing Observer-Observable (inheritance vs. composition)

Since Java 9 the pair Observer-Observable have been declared deprecated.

They don't provide a rich enough event model for applications. 
For example, they support only the notion that something has changed, 
but they don't convey any information about what has changed. 
There are also some thread-safety and sequencing issues that cannot be fixed compatibly. 

[...]

Observable has fallen into disuse and is no longer under active development.

Personally I used to show the Observer-Observable in classroom to provide an example of how interfaces can be used for callback-like behavior.

The deprecation forced me to look into the alternatives suggested. The Bean event model is quite general and can be adatped to the same use cases intended for the pair.

In addition comparing the two solutions we can observe two ditinct approaches to reused: inheritance vs. composition.

package observer.beans;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class Concerned implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Variation of " + evt.getPropertyName());
System.out.println("\t(" + evt.getOldValue() +
" -> " + evt.getNewValue() + ")");
System.out.println("Property in object " + evt.getSource());
}
}
package observer.beans;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Subject {
String property="initial";
// contain a support object instead of extending the support class
PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public void addObserver(PropertyChangeListener l) {
pcs.addPropertyChangeListener("theProperty", l);
}
public void setProperty(String val) {
String old = property;
property = val;
pcs.firePropertyChange("theProperty", old, val);
}
public String toString() { return "The subject object"; };
}
public class EsObserver {
public static void main(String[] args) {
Subject s = new Subject();
Concerned o = new Concerned();
s.addObserver(o);
s.setProperty("new");
}
}
package observer.oo;
import java.util.Observable;
import java.util.Observer;
public class Concerned implements Observer {
@Override
public void update(Observable src, Object arg) {
System.out.println("Variation of " + arg);
System.out.println("Property in object " + src);
}
}
package observer.oo;
import java.util.Observable;
public class Subject extends Observable {
String property="initial";
public void setProperty(String val) {
property = val;
setChanged();
notifyObservers("theProperty");
}
public String toString() { return "The subject object"; };
}
@ucchino
Copy link

ucchino commented Nov 5, 2019

interesting post, I find it useful to manage the problem of the observer applied to httpclient to analyze the stream of bytes transferred. A real use case can be founde here : https://www.baeldung.com/httpclient-post-http-request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment