How to combine two live data one after the other?

You can use my helper method: val profile = MutableLiveData<ProfileData>() val user = MutableLiveData<CurrentUser>() val title = profile.combineWith(user) { profile, user -> “${profile.job} ${user.name}” } fun <T, K, R> LiveData<T>.combineWith( liveData: LiveData<K>, block: (T?, K?) -> R ): LiveData<R> { val result = MediatorLiveData<R>() result.addSource(this) { result.value = block(this.value, liveData.value) } result.addSource(liveData) { result.value = … Read more

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

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

Mediator Vs Observer Object-Oriented Design Patterns

The Observer pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. The Mediator pattern: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you … 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