Share
Embed
Architecture Patterns in Practice with Kotlin
Kotlin is here and some Architecture Patterns need to be adapted.
From Karumi we've dedicated last 3 years talking about architecture and design patterns, each solution has pros and cons and could be good for different problems and kind of applications.
Kotlin brings to us different tools and ways to write code these can help us to write different patterns or apply different technics to that patterns.
In this talk, we present some commons problems in app designs and how we can provide solutions in Kotlin.
Karumi
November 25, 2017
Transcript
76 slides
-
Architecture Patterns in Practice with Kotlin Jorge Juan Barroso Carmona
Developer and GDE [email protected] @flipper83 -
Testing for android & iOS. Trainings Architecture, Patterns and principles
for Android & iOS. Mastering Git. Advanced mobile development. Testing for android & iOS. Architecture, Patterns and principles for Android & iOS. Companies For Everybody -
Sergio Gutierrez Senior Full Stack Engineer Pedro Gomez Senior Full
Stack Engineer Davide Mendolia Senior Full Stack Engineer Fran Toro Senior Mobile Engineer -
Adam Tornhill “Computer languages differ not so much in what
they make possible, but in what they make easy.” Larry Wall -
Activity Fragment Service View … Presenter View Use Case Rich
Model Repository DataSource Api Retrofit SharedPref Api Sync Execution -
Adam Tornhill class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val
presenter: SuperHeroesPresenter by injector.instance() private lateinit var adapter: SuperHeroesAdapter override val layoutId: Int = R.layout.main_activity override val toolbarView: Toolbar get() = toolbar /*…*/ } -
Adam Tornhill class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val
presenter: SuperHeroesPresenter by injector.instance() private lateinit var adapter: SuperHeroesAdapter override val layoutId: Int = R.layout.main_activity override val toolbarView: Toolbar get() = toolbar /*…*/ } Computed properties -
Adam Tornhill abstract class BaseActivity : KodeinAppCompatActivity(), LifecyclePublisher by lifeCycleLinker
{ abstract val layoutId: Int abstract val presenter: LifecycleSubscriber abstract val toolbarView: Toolbar abstract val activityModules: Module /*…*/ } -
Adam Tornhill class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val
presenter: SuperHeroesPresenter by injector.instance() private lateinit var adapter: SuperHeroesAdapter override val layoutId: Int = R.layout.main_activity override val toolbarView: Toolbar get() = toolbar /*…*/ } override properties -
Adam Tornhill class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val
presenter: SuperHeroesPresenter by injector.instance() private lateinit var adapter: SuperHeroesAdapter override val layoutId: Int = R.layout.main_activity override val toolbarView: Toolbar get() = toolbar /*…*/ } -
Adam Tornhill abstract class BaseActivity : KodeinAppCompatActivity(), LifecyclePublisher by lifeCycleLinker
{ abstract val layoutId: Int abstract val presenter: LifecycleSubscriber abstract val toolbarView: Toolbar abstract val activityModules: Module /*…*/ } Delegate -
Adam Tornhill abstract class BaseActivity : KodeinAppCompatActivity(), LifecyclePublisher by lifeCycleLinker
{ abstract val presenter: LifecycleSubscriber /*…*/ override fun onResume() { super.onResume() update() } override fun onDestroy() { unregister(presenter) super.onDestroy() } delegate to.. -
LifecycleLinker : LifecyclePublisher private val receivers = ArrayList<LifecycleSubscriber>() override fun
unregister (subscriber: LifecycleSubscriber) { receivers.remove(subscriber) } override fun update() { receivers .forEach (LifecycleSubscriber::update) } /*…*/ } Adam Tornhill override fun onResume() { super.onResume() update() } override fun onDestroy() { unregister(presenter) super.onDestroy() } BaseActivity : LifecyclePublisher by lifeCycleLinker -
Adam Tornhill class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes)
:LifecycleSubscriber { private val view: View? by weak(view) /*…*/ } -
Adam Tornhill class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes)
:LifecycleSubscriber { private val view: View? by weak(view) /*…*/ } argument properties -
Adam Tornhill class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes)
:LifecycleSubscriber { private val view: View? by weak(view) /*…*/ } argument properties delegate property -
fun <T> weak(value: T) = WeakRef(value) class WeakRef<out T>(value: T)
{ private val weakReference: WeakReference<T> = WeakReference(value) operator fun getValue(thisRef: Any, property: KProperty<*>): T? = weakReference.get() } Adam Tornhill class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) :LifecycleSubscriber { private val view: View? by weak(view) /*…*/ } argument poperties -
fun <T> weak(value: T) = WeakRef(value) class WeakRef<out T>(value: T)
{ private val weakReference: WeakReference<T> = WeakReference(value) operator fun getValue(thisRef: Any, property: KProperty<*>): T? = weakReference.get() } Adam Tornhill class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) :LifecycleSubscriber { private val view: View? by weak(view) /*…*/ } -
class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) : LifecycleSubscriber
{ private val view: View? by weak(view) override fun update() { view?.showLoading() refreshSuperHeroes() } We’re acting (Optional) -
fun <T> weak(value: T) = WeakRef(value) class WeakRef<out T>(value: T)
{ private val weakReference: WeakReference<T> = WeakReference(value) operator fun getValue(thisRef: Any, property: KProperty<*>): T? = weakReference.get() } Adam Tornhill class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) :LifecycleSubscriber { private val view: View? by weak(view) /*…*/ } -
A monad is just a monoid in the category of
endofunctors -
class NetworkSuperHeroDataSource( private val apiClient: CharacterApiClient) : SuperHeroDataSource { override
fun get(key: String): Either<DomainError, SuperHero> = try { mapResponse(apiClient.getCharacter(key)) { mapSuperHero(it) } } catch (exception: Exception) { Either.left(mapException(exception)) } /*…*/ } Adam Tornhill -
class NetworkSuperHeroDataSource( private val apiClient: CharacterApiClient) : SuperHeroDataSource { override
fun get(key: String): Either<DomainError, SuperHero> = try { mapResponse(apiClient.getCharacter(key)) { mapSuperHero(it) } } catch (exception: Exception) { Either.left(mapException(exception)) } /*…*/ } Adam Tornhill -
class NetworkSuperHeroDataSource( private val apiClient: CharacterApiClient) : SuperHeroDataSource { override
fun get(key: String): Either<DomainError, SuperHero> = try { mapResponse(apiClient.getCharacter(key)) { mapSuperHero(it) } } catch (exception: Exception) { Either.left(mapException(exception)) } /*…*/ } Adam Tornhill -
GetSuperHeroes: fun invoke(): Either<DomainError, List<SuperHero>> SuperHeroesRepository: fun getAllSuperHeroes(): Either<DomainError, List<SuperHero>>
SuperHeroDataSource: fun getAll(): Either<DomainError, List<SuperHero>> Adam Tornhill -
GetSuperHeroes: fun invoke(): Either<DomainError, List<SuperHero>> SuperHeroesRepository: fun getAllSuperHeroes(): Either<DomainError, List<SuperHero>>
SuperHeroDataSource: fun getAll(): Either<DomainError, List<SuperHero>> Adam Tornhill Don’t break the chain -
private fun refreshSuperHeroes() = async { val result = await
{ getSuperHeroes() } view?.hideLoading() when (result) { is Right -> showSuperHeroes(result.r) is Left -> view?.showError(result.l) } } Adam Tornhill MainPresenter: switch on steroids -
private fun refreshSuperHeroes() = async { val result = await
{ getSuperHeroes() } view?.hideLoading() when (result) { is Right -> showSuperHeroes(result.r) is Left -> view?.showError(result.l) } } Adam Tornhill MainPresenter: smart cast -
abstract class BaseActivity : KodeinAppCompatActivity(), LifecyclePublisher by lifeCycleLinker { /*…*/
fun showError(error: DomainError) = Snackbar.make(toolbarView, error.asString(this), Snackbar.LENGTH_LONG).show() } Adam Tornhill -
abstract class BaseActivity : KodeinAppCompatActivity(), LifecyclePublisher by lifeCycleLinker { /*…*/
fun showError(error: DomainError) = Snackbar.make(toolbarView, error.asString(this), Snackbar.LENGTH_LONG).show() } Adam Tornhill -
fun DomainError.asString(context: Context): String = context.getString(getMessage(this)) fun getMessage(domainError: DomainError): Int
= when (domainError) { is NotInternetDomainError -> R.string.error_not_internet_message is NotIndexFoundDomainError -> R.string.error_superhero_not_found_message is AuthDomainError -> R.string.error_invalid_credentials is UnknownDomainError -> R.string.error_unknown_message } Adam Tornhill extension method -
fun DomainError.asString(context: Context): String = context.getString(getMessage(this)) fun getMessage(domainError: DomainError): Int
= when (domainError) { is NotInternetDomainError -> R.string.error_not_internet_message is NotIndexFoundDomainError -> R.string.error_superhero_not_found_message is AuthDomainError -> R.string.error_invalid_credentials is UnknownDomainError -> R.string.error_unknown_message } Adam Tornhill -
fun DomainError.asString(context: Context): String = context.getString(getMessage(this)) fun getMessage(domainError: DomainError): Int
= when (domainError) { is NotInternetDomainError -> R.string.error_not_internet_message is NotIndexFoundDomainError -> R.string.error_superhero_not_found_message is AuthDomainError -> R.string.error_invalid_credentials is UnknownDomainError -> R.string.error_unknown_message } Adam Tornhill Compile error!! -
sealed class DomainError object NotInternetDomainError : DomainError() data class UnknownDomainError(
val errorMessage: String = "Unknown Error") : DomainError() data class NotIndexFoundDomainError(val key: String) : DomainError() object AuthDomainError : DomainError() Adam Tornhill -
class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) : LifecycleSubscriber
{ private fun refreshSuperHeroes() = async { val result = await { getSuperHeroes() } view?.hideLoading() when (result) { is Right -> showSuperHeroes(result.r) is Left -> view?.showError(result.l) } } /*…*/ } Adam Tornhill is this a method? or is an object? -
class GetSuperHeroes( private val superHeroesRepository: SuperHeroRepository) { operator fun invoke():
Either<DomainError, List<SuperHero>> = superHeroesRepository .getAllSuperHeroes() } Adam Tornhill Override operators getSuperHeroes() -
data class SuperHero( val id: String, val name: String, val
photo: String? = null, val isAvenger: Boolean, val description: String) Adam Tornhill -
data class SuperHero( val id: String, val name: String, val
photo: String? = null, val isAvenger: Boolean, val description: String) Adam Tornhill All domain models in the same file -
Activity Fragment Service View … Presenter View Use Case Rich
Model Repository DataSource Api Retrofit SharedPref Api Sync Execution -
class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) : LifecycleSubscriber
{ private fun refreshSuperHeroes() = async { val result = await { getSuperHeroes() } view?.hideLoading() when (result) { is Right -> showSuperHeroes(result.r) is Left -> view?.showError(result.l) } } /*…*/ } -
class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) : LifecycleSubscriber
{ private fun refreshSuperHeroes() = async { val result = await { getSuperHeroes() } view?.hideLoading() when (result) { is Right -> showSuperHeroes(result.r) is Left -> view?.showError(result.l) } } /*…*/ } -
class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes) : LifecycleSubscriber
{ private fun refreshSuperHeroes() = async { val result = await { getSuperHeroes() } view?.hideLoading() when (result) { is Right -> showSuperHeroes(result.r) is Left -> view?.showError(result.l) } } /*…*/ } -
class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val presenter: SuperHeroesPresenter
by injector.instance() /*...*/ override val activityModules = Module(allowSilentOverride = true) { bind<SuperHeroesPresenter>() with provider { SuperHeroesPresenter(this@MainActivity, instance()) } bind<GetSuperHeroes>() with provider{ GetSuperHeroes(instance()) } } -
class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val presenter: SuperHeroesPresenter
by injector.instance() /*...*/ override val activityModules = Module(allowSilentOverride = true) { bind<SuperHeroesPresenter>() with provider { SuperHeroesPresenter(this@MainActivity, instance()) } bind<GetSuperHeroes>() with provider{ GetSuperHeroes(instance()) } } -
class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val presenter: SuperHeroesPresenter
by injector.instance() /*...*/ override val activityModules = Module(allowSilentOverride = true) { bind<SuperHeroesPresenter>() with provider { SuperHeroesPresenter(this@MainActivity, instance()) } bind<GetSuperHeroes>() with provider{ GetSuperHeroes(instance()) } } -
class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override val presenter: SuperHeroesPresenter
by injector.instance() /*...*/ override val activityModules = Module(allowSilentOverride = true) { bind<SuperHeroesPresenter>() with provider { SuperHeroesPresenter(this@MainActivity, instance()) } bind<GetSuperHeroes>() with provider{ GetSuperHeroes(instance()) } } -
Bibliography They’re the cracks! Clean Code. Uncle Bob. 2008 Kotlin
For Android Developers. Antonio Leiva. 2015 GitHub AsyncAwait . metalabdesign. Github funKTionale. MarioAriasC. Kodein. Salomon Brys. Karumi Blog and Katas on github.