RxTUDelft/RxJavaGames — pull request #6 by rvanheest (2014-05-06 08:45:05+00:00)
Title: Tic tac toe

TicTacToe for now is implemented as a multiplayer game (if we have time at the end of this period, we will probably implement an AI player) and was mainly created to explore how RxJava can handle the situation of an MVC architecture where the model has mutable state (i.e. programming with side effects). In the 'old days' people used the GoF Observer-Observable Pattern to let the model signal to its observers that it had changed. Since RxJava's Observables (which are quite different from the GoF's) are the new way to go, we have to figure out a way how to replace the 'old stuff'.

The main challenge here was to get the `PublishSubject` integrated into the model classes, since we cannot inherit from it (which after all makes a lot of sence) and also not use it as a field within the class, since this field is not initialized when we need it: at the time when the `super`-constructor is called. I therefore came up with the following:

```Java
public class A extends Observable {

private B b;
private final PublishSubject subject;

public A(B b) {
this(B b, PublishSubject.create());
}

private A(B b, PublishSubject subject) {
super(subscriber -> subject.subscribe(subscriber));
this.b = b;
this.subject = subject;
}

public void setB(B b) {
this.b= b;

this.subject.onNext(this);
}

//some other methods
}
```

This method of using the `PublishSubject` corresponds to the Rx Design Guidelines (item 5.10) although this reference was found a few days later than this solution.