62
@giorgionatili #DroidconBos - #MobileTea Reactive MVP

Reactive MVP - Giorgio Natili - Codemotion Rome 2017

Embed Size (px)

Citation preview

Page 1: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Reactive MVP

Page 2: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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)

Page 3: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Agenda

+ Model View Presenter

+ Functional Reactive Programming

+ RxJava and Multi Threading

+ RxKotlin

Page 4: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 5: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Model View Presenter

Page 6: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Model View

Presenter

Page 7: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Model View Presenter

Page 8: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Model View Presenter

Page 9: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Original Design

Page 10: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Variations

Page 11: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

The Presenter

Page 12: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Presenter Responsibilities

+ Manage the business logic

+ Handle the interaction between components

+ Orchestrator of behaviors and communication

Page 13: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 14: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Testable

+ Methods should be easy to be tested

+ Optionally (but strongly encouraged) should return a value

Page 15: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

The View

Page 16: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

View Responsibilities

+ Rendering data on the screen

+ Handle user interaction

+ Consume behaviors trough the presenter

Page 17: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 18: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

The Model

Page 19: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 20: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Model Traits

+ Decoupled and, when possible, framework agnostic

+ Designed to manage concurrency

+ Easy to test without external dependencies

Page 21: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Designing the Model

Page 22: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Behaviors

+ Identify software behaviors

+ Group functionality by behavior

+ Consistent returned data types

Page 23: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 24: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Entities

+ Should have a unique identity

+ Should delegate behaviors to value objects and aggregates

Page 25: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 26: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Repositories

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

+ Usually injected in a Service

Page 27: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Equence

+ Prevent race conditions and data collisions

+ Unify domain objects in aggregates

Page 28: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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.

Page 29: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Page 30: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Functional Reactive Programming

Page 31: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 32: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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! : )

Page 33: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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>

Page 34: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 35: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Pull vs Push

Page 36: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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)

Page 37: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Page 38: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

RxJava

Page 39: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 40: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Create an Observable

Page 41: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Subscribe to Observable

Page 42: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 43: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 44: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 45: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 46: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

observeOn()

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

Page 47: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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)

Page 48: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Page 49: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Kotlin

Page 50: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Why Kotlin+ Data classes

+ Inferred datatypes

+ Nullable types

+ Arguments default value

+ Lambda expression and “elvis” operator

+ Extension functions

Page 51: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Kotlin and the JVM

Page 52: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

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

Page 53: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Data Classes

Page 54: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Custom Getters

Page 55: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 56: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@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

Page 57: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

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

parameters, or returns a function

Page 58: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Page 59: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Clean Architecture

Page 60: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Demo

Page 61: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Questions & Answers

Page 62: Reactive MVP - Giorgio Natili - Codemotion Rome 2017

@giorgionatili#DroidconBos - #MobileTea

Thanks!