Reactive MVP - Giorgio Natili - Codemotion Rome 2017

Preview:

Citation preview

@giorgionatili#DroidconBos - #MobileTea

Reactive MVP

@giorgionatili#DroidconBos - #MobileTea

About Me

+ Engineering Manager at Akamai (NG2 on desktop and mobile)

+ Founder of Mobile Tea (www.meetup.com/pro/mobiletea)

+ Organizer of DroidCon Boston (www.droidcon-boston.com)

@giorgionatili#DroidconBos - #MobileTea

Agenda

+ Model View Presenter

+ Functional Reactive Programming

+ RxJava and Multi Threading

+ RxKotlin

@giorgionatili#DroidconBos - #MobileTea

Before We Start

+ There is a slide that is a fake

+ Catch it and tweet it (hashtag #DroidconBos)

+ Win a free ticket to Droidcon Boston

@giorgionatili#DroidconBos - #MobileTea

Model View Presenter

@giorgionatili#DroidconBos - #MobileTea

Model View

Presenter

@giorgionatili#DroidconBos - #MobileTea

Model View Presenter

@giorgionatili#DroidconBos - #MobileTea

Model View Presenter

@giorgionatili#DroidconBos - #MobileTea

Original Design

@giorgionatili#DroidconBos - #MobileTea

Variations

@giorgionatili#DroidconBos - #MobileTea

The Presenter

@giorgionatili#DroidconBos - #MobileTea

Presenter Responsibilities

+ Manage the business logic

+ Handle the interaction between components

+ Orchestrator of behaviors and communication

@giorgionatili#DroidconBos - #MobileTea

Presenter Traits

+ A tiny class with a single responsibility

+ Optionally exposed trough an interface

+ Self independent and free from framework specific constraints

@giorgionatili#DroidconBos - #MobileTea

Testable

+ Methods should be easy to be tested

+ Optionally (but strongly encouraged) should return a value

@giorgionatili#DroidconBos - #MobileTea

The View

@giorgionatili#DroidconBos - #MobileTea

View Responsibilities

+ Rendering data on the screen

+ Handle user interaction

+ Consume behaviors trough the presenter

@giorgionatili#DroidconBos - #MobileTea

View Traits

+ A class that deals only with the presentation layer

+ Always exposed trough an interface to the presenter

+ Self independent but able to handle frameworks constraints

@giorgionatili#DroidconBos - #MobileTea

The Model

@giorgionatili#DroidconBos - #MobileTea

Model Responsibilities

+ Manage the domain entities of a software

+ Facilitate the interaction with specific domain entities

+ Abstract domain objects in the context of a specific view

@giorgionatili#DroidconBos - #MobileTea

Model Traits

+ Decoupled and, when possible, framework agnostic

+ Designed to manage concurrency

+ Easy to test without external dependencies

@giorgionatili#DroidconBos - #MobileTea

Designing the Model

@giorgionatili#DroidconBos - #MobileTea

Behaviors

+ Identify software behaviors

+ Group functionality by behavior

+ Consistent returned data types

@giorgionatili#DroidconBos - #MobileTea

Value Objects

+ Should represent a value in your system

+ Should describe the characteristics of a thing

+ Equality is not based upon identity

+ Should be immutable

@giorgionatili#DroidconBos - #MobileTea

Entities

+ Should have a unique identity

+ Should delegate behaviors to value objects and aggregates

@giorgionatili#DroidconBos - #MobileTea

Aggregates

+ A cluster of associated objects that are treated as a unit for the purpose of data change

+ A way to move as much as possible of the behavior away from the Entities within the aggregate -> into Value Objects

+ A place to define the consistency rules of Value Objects

@giorgionatili#DroidconBos - #MobileTea

Repositories

+ A mechanism for encapsulating storage, retrieval, and search behaviors which emulates a collection of objects

+ Usually injected in a Service

@giorgionatili#DroidconBos - #MobileTea

Equence

+ Prevent race conditions and data collisions

+ Unify domain objects in aggregates

@giorgionatili#DroidconBos - #MobileTea

Services

+ Services are always exposed as an interface, not for “swappability” or testability, but to expose a set of cohesive operations

+ Domain services are the coordinators, allowing higher level functionality between many different smaller parts.

@giorgionatili#DroidconBos - #MobileTea

@giorgionatili#DroidconBos - #MobileTea

Functional Reactive Programming

@giorgionatili#DroidconBos - #MobileTea

In a Nutshell

+ FRP is a programming paradigm

+ It focuses on reacting to streams of changes (data and events)

+ It favors function composition, avoidance of global state and side effects

@giorgionatili#DroidconBos - #MobileTea

FRP Use Cases

+ Processing user (tap, swipe, etc.) and system events (GPS, gyroscope, etc.)

+ Responding and processing any IO event (asynchronously)

+ Handling events and data pushed from other sources

+ Many, many, many others! : )

@giorgionatili#DroidconBos - #MobileTea

Observable<T>

+ An Observable represents a flowing stream of values

+ UI events

+ Data processed in background

+ The most similar abstraction is Iterable<T>

@giorgionatili#DroidconBos - #MobileTea

Pull vs Push

+ Observable is push based, it decides when to emit values

+ Iterable is pull based, it sits until the some ask for the next() value

@giorgionatili#DroidconBos - #MobileTea

Pull vs Push

@giorgionatili#DroidconBos - #MobileTea

Hot vs Cold Observables

+ A cold Observable is entirely lazy

+ Hot Observables might already be emitting events no matter how many Subscribers they have

+ Hot Observables occur when we have absolutely no control over the source of events (aka taps)

@giorgionatili#DroidconBos - #MobileTea

@giorgionatili#DroidconBos - #MobileTea

RxJava

@giorgionatili#DroidconBos - #MobileTea

In a Nutshell

+ It’s an implementation of the Reactive Extensions (Rx) on the JVM

+ In RxJava, events are modeled as observable streams to which observers are subscribed

+ Implement methods composibility to improve the management of complicated workflows

@giorgionatili#DroidconBos - #MobileTea

Create an Observable

@giorgionatili#DroidconBos - #MobileTea

Subscribe to Observable

@giorgionatili#DroidconBos - #MobileTea

Multithreading

+ The ability to run several functions of a single program simultaneously

+ Predominantly by utilizing several processors, but potentially, by time-sharing their resource requirements

@giorgionatili#DroidconBos - #MobileTea

Parallelization

+ A common question about RxJava is how to achieve parallelization, or emitting multiple items concurrently from an Observable

+ This definition breaks the Observable contract which states that onNext() must be called sequentially and never concurrently

@giorgionatili#DroidconBos - #MobileTea

RxJava Implementation

+ Version 2.0.5 introduced the ParallelFlowable API that allows parallel execution of a few select operators such as map, filter, concatMap, reduce and so on

+ Earlier versions introduced observeOn and subscribeOn to model parallel computations

@giorgionatili#DroidconBos - #MobileTea

subscribeOn()

+ The method subscribeOn() allows you to move the execution of the Observable code into another thread and with an specific behavior

@giorgionatili#DroidconBos - #MobileTea

observeOn()

+ The observeOn() method allows to specify where to execute the functions provided in the subscribe() one

@giorgionatili#DroidconBos - #MobileTea

Schedulers+ immediate(), creates and returns a Scheduler that executes work immediately on

the current thread

+ trampoline(), creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes

+ newThread(), creates and returns a Scheduler that creates a new Thread for each unit of work

+ computation(), creates and returns a Scheduler intended for computational work

+ io(), creates and returns a Scheduler intended for 10-bound work (unbounded, multiple threads)

@giorgionatili#DroidconBos - #MobileTea

@giorgionatili#DroidconBos - #MobileTea

Kotlin

@giorgionatili#DroidconBos - #MobileTea

Why Kotlin+ Data classes

+ Inferred datatypes

+ Nullable types

+ Arguments default value

+ Lambda expression and “elvis” operator

+ Extension functions

@giorgionatili#DroidconBos - #MobileTea

Kotlin and the JVM

@giorgionatili#DroidconBos - #MobileTea

Nullable Types+ Nothing can be null in Kotlin until is not specified

@giorgionatili#DroidconBos - #MobileTea

Data Classes

@giorgionatili#DroidconBos - #MobileTea

Custom Getters

@giorgionatili#DroidconBos - #MobileTea

Lambdas+ Small chunks of code that can be passed to other functions

+ The ability to treat functions as values is part of the functional programming paradigm

@giorgionatili#DroidconBos - #MobileTea

Inline Functions + Default Values

+ Functions can be declared outside of a class and imported in others

+ Arguments can have default values reducing the number of methods overloads

@giorgionatili#DroidconBos - #MobileTea

High Order Functions+ A higher-order function is a function that takes functions as

parameters, or returns a function

@giorgionatili#DroidconBos - #MobileTea

@giorgionatili#DroidconBos - #MobileTea

Clean Architecture

@giorgionatili#DroidconBos - #MobileTea

Demo

@giorgionatili#DroidconBos - #MobileTea

Questions & Answers

@giorgionatili#DroidconBos - #MobileTea

Thanks!