Skip to content

Instantly share code, notes, and snippets.

@jeffypooo
Created November 20, 2019 19:05
Show Gist options
  • Save jeffypooo/7eebc98ba6568b374e55c6935ef2dca0 to your computer and use it in GitHub Desktop.
Save jeffypooo/7eebc98ba6568b374e55c6935ef2dca0 to your computer and use it in GitHub Desktop.
Usecase base classes
import io.reactivex.Observable
import io.reactivex.Single
/**
* Something that can be executed.
* @param ArgsType The argument data type.
* @param ReturnType The return data type for the execution
*/
interface AbstractExecutable<ArgsType : Any, ReturnType : Any> {
fun execute(data: ArgsType): ReturnType
}
/**
* Convenience extension for [AbstractExecutable]s with [Unit] argument types.
*/
fun AbstractExecutable<Unit, *>.execute() = execute(data = Unit)
/**
* Base use-case types for the application.
*/
sealed class UseCase<A : Any, R : Any> : AbstractExecutable<A, R> {
/**
* Synchronous use-case. Executes and returns the result immediately.
*/
abstract class Synchronous<A : Any, R : Any> : UseCase<A, R>()
/**
* Reactive use-case. Returns an Observable stream of results.
* @param R the element type for the [Observable].
*/
abstract class RxObservable<A : Any, R : Any> : UseCase<A, Observable<R>>()
/**
* Reactive use-case. Returns a Single that emits the result type.
* @param R the element type for the [Single].
*/
abstract class RxSingle<A: Any, R: Any>: UseCase<A, Single<R>>()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment