--
You received this message because you are subscribed to the Google Groups "RxJava" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rxjava+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rxjava/c42c89a3-3b41-419a-af5c-4fba4ca0c19bn%40googlegroups.com.
/*
* Simple Obesrver Example
*/
package demos;
/**
* @author guthrie
*/
import java.util.Observable;
import java.util.Observer;
public class ObserverDemo {
public static void main(String[] args){
Employee e = new Employee();
Manager m = new Manager();
e.addObserver(m); // register for observing
e.setSalary(100.0f);
}
}
// ----------------------------------------------------------
class Employee extends Observable {
float salary;
float getSalary() {
return salary;
}
public void setSalary(float newSalary){
salary=newSalary; // salary has changed
setChanged(); // mark that this object has changed
notifyObservers(new Float(salary)); // notify all observers
}
}
// ----------------------------------------------------------
class Manager implements Observer {
public void update(Observable obj, Object arg){ // Ugh - untyped Java inerface
System.out.println( obj.getClass().getName() + " changed and new salary is " +
(Float)arg ); // Boo! Downcast!
}
}
Dr. Gregory Guthrie
Thanks very much – will try it. I haven’t seen that before.
Most examples I have seen are either creating Observables from fixed structures (lists, …), or with embedded anonymous Observers in a lambda.
But from my readings the .create factory seems like it should be useable as I tried, yes? Having it reference a method which registers the observer for further events.
Thanks. On looking at this, the main difference is that this is using new local anonymous objects, and my example was trying to link two existing external classes (which I only showed as skeletons, but would have a bunch of other logic & methods).
That is why I was trying to use the create(registration.function) factory and Observer/Observable inheritances, similar to the old Java approach. I want to take two existing classes, and then modify and link them as Observer/Observable.
Sounds conceptually easy – but as noted I didn’t get it to work (yet!).
I did mention the type errors I am getting - but the type signatures are perplexing to me - that I have seems like it should work with the specifications.
Thanks,
Gregory
To view this discussion on the web visit https://groups.google.com/d/msgid/rxjava/1f75d93a-e0bb-42df-8d59-67d086d7cc4cn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rxjava/3a26f5d0-a1bb-457e-a063-37aca52d0279n%40googlegroups.com.