Difference between Observer, Pub/Sub, and Data Binding

There are two major differences between Observer/Observable and Publisher/Subscriber patterns: Observer/Observable pattern is mostly implemented in a synchronous way, i.e. the observable calls the appropriate method of all its observers when some event occurs. The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue). In the Observer/Observable pattern, the observers are aware … Read more

Observer is deprecated in Java 9. What should we use instead of it?

Why is that? Does it mean that we shouldn’t implement observer pattern anymore? Answering the latter part first – YES, it does mean you shouldn’t implement Observer and Obervables anymore. Why were they deprecated – They didn’t provide a rich enough event model for applications. For example, they could support only the notion that something … Read more

Leveraging the observer pattern in JavaFX GUI design

As noted here, the JavaFX architecture tends to favor binding GUI elements via classes that implement the Observable interface. Toward this end, Irina Fedortsova has adapted the original Converter to JavaFX in Chapter 5 of JavaFX for Swing Developers: Implementing a Swing Application in JavaFX. I’ve recapitulated the program below, updating to Java 8 and … Read more

Super-simple example of C# observer/observable with delegates

The observer pattern is usually implemented with events. Here’s an example: using System; class Observable { public event EventHandler SomethingHappened; public void DoSomething() => SomethingHappened?.Invoke(this, EventArgs.Empty); } class Observer { public void HandleEvent(object sender, EventArgs args) { Console.WriteLine(“Something happened to ” + sender); } } class Test { static void Main() { Observable observable = … Read more