Subject/Observer
Subject/Observer模式,Subject翻译为主题 , Subject可以支持一个事件流发送过程中,多个的观察者一起订阅。Subject有四个实现类:AsyncSubject,BehaviorSubject,PublishSubject,ReplaySubject。
值得注意的是一定要用Subcect.create()的方式创建并使用,不要用just(T)、from(T)、create(T)创建,否则会导致失效,因为just(T)、from(T)、create(T)会把Subject转换为Obserable
1.AsyncSubject
简单的说使用AsyncSubject无论输入多少参数,永远只输出最后一个参数。
private Observer<Integer> getObserver(final String id,final String tag) {
return new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
print(tag+"-->"+id+" onSubscribe");
}
@Override
public void onNext(Integer value) {
print(tag+"-->"+id+" onNext : value :"+value);
}
@Override
public void onError(Throwable e) {
print(tag+"-->"+id+" onError : " + e.getMessage());
}
@Override
public void onComplete() {
print(tag+"-->"+id+" onComplete " );
}
};
}
AsyncSubject<Integer> source = AsyncSubject.create();
source.subscribe(getObserver("First","AsyncSubject"));
// it will emit only 4 and onComplete
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 4 and onComplete for second observer also.
*/
source.subscribe(getObserver("Second","AsyncSubject"));
source.onNext(4);
source.onComplete();
/*
* it will emit nothing.
*/
source.subscribe(getObserver("Third","AsyncSubject"));
输出结果:
AsyncSubject-->First onSubscribe
AsyncSubject-->Second:onSubscribe
AsyncSubject-->First onNext : value :4
AsyncSubject-->First onComplete
AsyncSubject-->Second onNext : value :4
AsyncSubject-->Second onComplete
AsyncSubject-->Third:onSubscribe
AsyncSubject-->Third onNext : value :4
AsyncSubject-->Third onComplete
2.BehaviorSubject
BehaviorSubject会发送离订阅最近的上一个值,没有上一个值的时候会发送默认值。
BehaviorSubject<Integer> source = BehaviorSubject.create();
source.subscribe(getObserver("First","BehaviorSubject"));
// it will get 1, 2, 3, 4 and onComplete
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 3(last emitted), 4 and onComplete for second observer also.
*/
source.subscribe(getObserver("Second","BehaviorSubject"));
source.onNext(4);
source.onComplete();
/*
* it will emit nothing .
*/
source.subscribe(getObserver("Third","BehaviorSubject"));
输出结果:
BehaviorSubject-->First onSubscribe
BehaviorSubject-->First onNext : value :1
BehaviorSubject-->First onNext : value :2
BehaviorSubject-->First onNext : value :3
BehaviorSubject-->Second:onSubscribe
BehaviorSubject-->Second onNext : value :3
BehaviorSubject-->First onNext : value :4
BehaviorSubject-->Second onNext : value :4
BehaviorSubject-->First onComplete
BehaviorSubject-->Second onComplete
BehaviorSubject-->Third:onSubscribe
BehaviorSubject-->Third onComplete
3.PublishSubject
可以说是最正常的Subject,从那里订阅就从那里开始发送数据。
PublishSubject<Integer> source = PublishSubject.create();
source.subscribe(getObserver("First","PublishSubject"));
// it will get 1, 2, 3, 4 and onComplete
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 4 and onComplete for second observer also.
*/
source.subscribe(getObserver("Second","PublishSubject"));
source.onNext(4);
source.onComplete();
/*
* it will emit nothing.
*/
source.subscribe(getObserver("Third",PublishSubject"));
输出结果:
PublishSubject-->First onSubscribe
PublishSubject-->First onNext : value :1
PublishSubject-->First onNext : value :2
PublishSubject-->First onNext : value :3
PublishSubject-->Second:onSubscribe
PublishSubject-->First onNext : value :4
PublishSubject-->Second onNext : value :4
PublishSubject-->First onComplete
PublishSubject-->Second onComplete
PublishSubject-->Third:onSubscribe
PublishSubject-->Third onComplete
4.ReplaySubject
无论何时订阅,都会将所有历史订阅内容全部发出。
ReplaySubject<Integer> source = ReplaySubject.create();
source.subscribe(getObserver("First","ReplaySubject")); // it will get 1, 2, 3, 4
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 1, 2, 3, 4 for second observer also as we have used replay
*/
source.subscribe(getObserver("Second","ReplaySubject"));
source.onNext(4);
source.onComplete();
/*
* it will emit 1, 2, 3, 4 for second observer also as we have used replay
*/
source.subscribe(getObserver("Third","ReplaySubject"));
输出结果:
ReplaySubject-->First onSubscribe
ReplaySubject-->First onNext : value :1
ReplaySubject-->First onNext : value :2
ReplaySubject-->First onNext : value :3
ReplaySubject-->Second:onSubscribe
ReplaySubject-->Second onNext : value :1
ReplaySubject-->Second onNext : value :2
ReplaySubject-->Second onNext : value :3
ReplaySubject-->First onNext : value :4
ReplaySubject-->Second onNext : value :4
ReplaySubject-->First onComplete
ReplaySubject-->Second onComplete
ReplaySubject-->Third:onSubscribe
ReplaySubject-->Third onNext : value :1
ReplaySubject-->Third onNext : value :2
ReplaySubject-->Third onNext : value :3
ReplaySubject-->Third onNext : value :4
ReplaySubject-->Third onComplete
总结
RxJava2.0的观察者模式总的来说应该就这些,只要会用Observable和Flowable就足够了,其他只要知道,或者不知道也没关系,在后续使用操作符的时候你就可以慢慢接触了。
所以没有提及RxJava1.0,
主要是因为:
1.原理上是一样的,提及了记得东西多,反而复杂了,还特别容易搞混。
2.Rxjava2是RaJava1的升级版本,方法名,安全性等要好很多学起来要比Rxjava1要容易理解。学习的时候优先选择RxJava2。
你可能一直纠结到底是学完RxJava1再看RxJava2,还是掌握RxJava2在了解RxJava1,因为毕竟很多项目都是掺杂着这两个。
我的建议是只要掌握RxJava2就可以了,至于遇到项目使用了RxJava1,只要你理解代码的逻辑了就可以了,如果涉及使用的不多,你可以将其切换为RxJava2来实现,毕竟在处理异常的时候,RxJava2要安全一些,如果涉及到很多逻辑就不建议改了。
接下来我会详细讲解RxJava2.0的6大类型操作符。小伙伴们赶快去安利一波,最后的最后,不要忘记点个赞哦!