Created
April 23, 2018 11:33
-
-
Save mtorchiano/e69ac7e309fee81bd17f4f0740b9ffa9 to your computer and use it in GitHub Desktop.
Revisions
-
mtorchiano created this gist
Apr 23, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"; }; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"; }; }