Skip to content

Instantly share code, notes, and snippets.

@baldraider
Last active March 24, 2019 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baldraider/28429b9612c465b24a19acbfc70c0a7b to your computer and use it in GitHub Desktop.
Save baldraider/28429b9612c465b24a19acbfc70c0a7b to your computer and use it in GitHub Desktop.
Implementing Observer<T> and Observable<T>
class ObservableObserverExample {
fun main(args : Array<String>){
val myObserver = MyObserver()
val myObservable = MyObservable()
myObservable.subscribe(myObserver)
}
class MyObserver : Observer<String>{
override fun onComplete() {
println("Sequences stream completed")
}
override fun onNext(value: String) {
println("Received value "+value)
}
override fun onError(e: Throwable) {
println("Error occurred while streaming sequences")
}
}
class MyObservable : Observable<String> {
override fun subscribe(observer : Observer<String>) : Disposable {
observer.onNext("a")
observer.onNext("b")
observer.onNext("c")
observer.onCompleted()
return Disposables.empty()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment