Created
March 19, 2017 04:19
-
-
Save Van1996/78919273817124805e4a1c49024a2979 to your computer and use it in GitHub Desktop.
RxJavaDemo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rx.Observable; | |
import rx.Observer; | |
import rx.functions.Func1; | |
/** | |
* Created by Van on 2017/03/13. | |
*/ | |
/* | |
依赖: | |
compile 'io.reactivex:rxjava:1.1.3' | |
compile 'io.reactivex:rxandroid:1.1.0' | |
*/ | |
public class DemoModule { | |
public static void main(String[] args) { | |
product(); | |
} | |
static void product() { | |
Phone[] phones = new Phone[5]; | |
for (int i = 0; i < phones.length; i++) { | |
phones[i] = new Phone(i + "号iPhone"); | |
} | |
Observable.from(phones) | |
.map(new Func1<Phone, Phone>() { //装天线加工 | |
@Override | |
public Phone call(Phone phone) { | |
try { | |
Thread.sleep(2000); //为了体现顺序,我给加工设置了耗时 | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return phone.change("装了天线的"); | |
} | |
}) | |
.map(new Func1<Phone, Phone>() { //装摄像头加工 | |
@Override | |
public Phone call(Phone phone) { | |
return phone.change("装了摄像头的、"); | |
} | |
}) | |
.subscribe(new Observer<Phone>() { //打包员 | |
@Override | |
public void onCompleted() { | |
System.out.println("搞掂,收工!"); | |
} | |
@Override | |
public void onError(Throwable e) { | |
System.out.println("出了点小问题"); | |
} | |
@Override | |
public void onNext(Phone phone) { | |
System.out.println(phone.show()+"被打包好了"); | |
} | |
}); | |
} | |
static class Phone { | |
String name; | |
Phone(String name) { | |
this.name = name; | |
} | |
Phone change(String change) { | |
name = change + name; | |
return this; | |
} | |
String show() { | |
return this.name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment