Thursday 20 March 2014

Wednesday - Android Livecode, RxJava Architectures on Android at Microsoft

This was an Android Livecode presentation hosted at the Microsoft offices in sunny Westminster.

Yeah, I didn't get it either, and the Microsoft people were not exactly convincing in their reasons for hosting what is, essentially, a presentation for the competition. (For those who don't know, Android is the mobile phone\embedded operating system owned by Google and in competition with Microsoft's Windows 8).

After the introduction, the main presentation was given by a Berliner, Timo Tuominen from Futurice, concerning the use of Functional Reactive programming on Android. This centred around the use of RxJava, an open source library based on a Microsoft library and implemented by Netflix, the video streaming company.



(A well attended presentation, as you can see, but there was free beer and pizza).

RxJava is centered around the use of an Observable class i.e. an implementation of the Observer design pattern from the "Gang of Four" book, on which most event models are based. It seems that the difference between, say, the standard Java listeners and RxJava is that the listeners have to be implemented within the class as you're coding it. Here's something I've been working on for the College:
//custom event maintenance
//this is a list of events that have to
//be serviced when something happens
private List<SwitchListener> listenerList = new ArrayList<SwitchListener>();

public void addSwitchListener(SwitchListener listener) {
 listenerList.add(listener);
}

public void removeSwitchListener(SwitchListener listener) {
 listenerList.remove(listener);
}

private void fireSwitchListener() {
 //parcel up the state of the switch in a SwitchEvent object
 SwitchEvent e = new SwitchEvent(this, isSwitchOn());
 //then send it to every listener in the list
 for(SwitchListener listener: listenerList) {
  listener.switchChange(e);
 }
}

Listeners are all over Java components as it provides a way of passing messages between the components and their containers without the components having references to the latter (a problem known as "coupling").

What RxJava seems to do is wrapper up listeners, or something like them, into a framework that can be applied to any other object. For example, if you want to "listen" to an object so that when it changes you can do something else, or react to it, you can use the Observable class to do it:

Observable.from(names).subscribe(new Action1<String>() {
 public void call(String s) {
  System.out.println("Hello " + s + "!");
 }
});

It does seem to be something that you would think was already in function programming languages, like Clojure and Scala, by default, but RxJava can be used by these, so maybe it isn't?

Overall, the presentation wasn't brilliant as Timo is not a natural presenter, plus he seemed unaware of any of the alternatives (although his boss was there and stepped in to answer the awkward question or two that arose), which was a shame because it does seem like an interesting subject.

No comments:

Post a Comment