Implement Observable pattern like MutableLiveData in Android
See original GitHub issueIs your feature request related to a problem? Please describe. This feature suggestion aims to solve the problem of listening for variable value change (normally managed with JavaRX) without the need of importing the whole JavaRX system.
Describe the solution you’d like It would be great to have a decorator that provides Observable features.
Describe alternatives you’ve considered There are currently no elegant solutions available:
- Observable pattern: verbose and now deprecated in Java
- JavaRX: a-way-too-wide system to import only for such a small problem
- PropertyChangeSupport: it works but it’s too verbose and scattered, it would be glad to have a wrapper for it
Additional context Below a potential working solution (without the decorators):
package it.muzzialessandro.utils;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.UUID;
public class Observable<T> {
private T value;
private final PropertyChangeSupport pcSupport;
private final String uuid;
public Observable(T value) {
this.value = value;
this.pcSupport = new PropertyChangeSupport(this);
this.uuid = UUID.randomUUID().toString();
this.pcSupport.addPropertyChangeListener(evt -> {
synchronized (this) {
this.notify();
}
});
}
//region Public Methods
public void addPropertyChangeListener(PropertyChangeListener pcl) {
this.pcSupport.addPropertyChangeListener(pcl);
}
public void removePropertyChangeListener(PropertyChangeListener pcl) {
this.pcSupport.removePropertyChangeListener(pcl);
}
public void listenForChange() {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//endregion
//region Getter and Setter
public T getValue() {
return value;
}
public synchronized void setValue(T value) {
this.pcSupport.firePropertyChange(this.uuid, this.value, value);
this.value = value;
}
public String getUuid() {
return this.uuid;
}
//endregion
}
Usage:
declaration:
Observable<String> strObs;
synchronous:
strObs.listenForChange();
asynchronous:
strObs.addPropertyChangeListener(evt -> doSomething());
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
LiveData overview - Android Developers
The Room persistence library supports observable queries, which return LiveData objects. Observable queries are written as part of a Database ...
Read more >How to implement observer pattern in android studio?
In Android, the observer pattern is implemented by using the class ViewModel and the class LiveData / StateFlow. If you want to have...
Read more >LiveData vs ObservableField in Android - MindOrks
In this blog, we will talk about LiveData vs ObservableField in Android. We will also talk about the differences and also compare them....
Read more >ViewModels and LiveData: Patterns + AntiPatterns - Medium
The most common use case for LiveData is using MutableLiveData in ViewModels and exposing them as LiveData to make them immutable from the ......
Read more >Observer Pattern In Kotlin
Also known as Publish / Subscribe Pattern, Signal-Slot Pattern. Part of Event-Driven Architecture. In this tutorial, we will show you how to implement...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I must have explained myself badly, my intention was to imitate the behavior of that Android class. I would really appreciate the lazy decorator, I’ve used lazy properties a couple of times in Kotlin. Honestly I didn’t understand the concept of “dynamic properties” but I will wait for news from you, count on me for the feedback 💪 If you prefer just send me a standard email instead of writing here, do as you prefer.
Yeah, great idea, keep me updated