Skip to content

Instantly share code, notes, and snippets.

@mtorchiano
Created April 23, 2018 11:33
Show Gist options
  • Select an option

  • Save mtorchiano/e69ac7e309fee81bd17f4f0740b9ffa9 to your computer and use it in GitHub Desktop.

Select an option

Save mtorchiano/e69ac7e309fee81bd17f4f0740b9ffa9 to your computer and use it in GitHub Desktop.

Revisions

  1. mtorchiano created this gist Apr 23, 2018.
    21 changes: 21 additions & 0 deletions _README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    ## 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.
    16 changes: 16 additions & 0 deletions beans.Concerned.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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());
    }

    }
    23 changes: 23 additions & 0 deletions beans.Subject.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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"; };
    }
    15 changes: 15 additions & 0 deletions main.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    public class EsObserver {

    public static void main(String[] args) {

    Subject s = new Subject();

    Concerned o = new Concerned();

    s.addObserver(o);

    s.setProperty("new");

    }

    }
    14 changes: 14 additions & 0 deletions oo.Concerned.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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);
    }

    }
    16 changes: 16 additions & 0 deletions oo.Subject.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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"; };
    }