0

I started learning Rx Java and have a simple question

I have a class Foo.class. Inside the class I have a variable Object mObject with getter and setter methods for mObject. In my other class Bar.class I will react when the value of mObject is changed. How can I implement it with Rx Java?

I tried this solution but I don't understand the code there Detecting value change using Rxjava

Taylor Buchanan
  • 4,155
  • 1
  • 28
  • 40
dudi
  • 5,523
  • 4
  • 28
  • 57

2 Answers2

0

You can go with Publish Subject. By this you can observe on single Int variable also.

Go here for documentation Publish Subject

Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22
0

Implement wrapper for Object class that will control access to that object and if something changes it will push event to Subject.

public class ObjectChanges extends Object or Implements its interface {
   private Object mObject;
   private Subject<Object> subject;

   <constructor that takes mObject>

   public void setField(String value) {
      mObject.setField(value);
      subject.onNext(mObject)  
   }

   public Observable<Object> subscribe() {
      return (Observable<Object>) this.subject;
   } 

   ...
}

You can use decorator pattern for it as well. I advice you to not add this logic Object because then it may cause a lot of architectural problems. For example you may want to implement another wrapper that will validate fields of that object, will you do it in Object too? On the other hand you will just implement that wrapper and use it where you want:

Object obj = new ValidationWrapper(new RxWrapper(new Object()));
gzaripov
  • 48
  • 1
  • 7