clean architecture on android

24
Clean Architecture on Android Benjamin / 班班班

Upload: chengwc

Post on 09-Jan-2017

1.111 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Clean architecture on android

Clean Architecture on AndroidBenjamin / 班傑明

Page 2: Clean architecture on android

A real-world app

Fragment Adapter

AsyncTask

Network API client

AsyncTask

View

View

Data model

Page 3: Clean architecture on android

Challenge

● Hard to understand/maintain○ The business logic is mixed in Activity/Fragment/Adapter

○ Loose cohesive, high coupled (低內聚,高耦合 )

● Hard to test○ Cannot fully test the logic in Activity/Fragment/Adapter

○ The UI testing is fragile. (Will build fail if there are content or server-side issues)

○ It costs around 30 minutes to finish our UI testing

○ It’s really frustrating and lose productivity when the tests keep failing (lots of 30+ minutes)

Page 4: Clean architecture on android

MVP

From: http://www.captechconsulting.com/blogs/a-mvp-approach-to-lifecycle-safe-requests-with-retrofit-20-and-rxjava

Page 5: Clean architecture on android

MVP - Interfaces

Program to an interface, not an implementation

Page 6: Clean architecture on android

MVP - View

Page 7: Clean architecture on android

MVP - View

Page 8: Clean architecture on android

MVP - Presenter

Page 9: Clean architecture on android

MVP - Model

● Model? NetworkAPIClient?

● Is MVP an Architecture pattern?

Page 10: Clean architecture on android

MVC/MVP/MVVM is NOT an Architecture

● MVP is not an architectural pattern, it’s only responsible for the presentation layer (delivery mechanism)

Page 11: Clean architecture on android

The Clean Architecture

From: http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html

Page 12: Clean architecture on android

The Clean Architecture (conti.)

From Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg

Page 13: Clean architecture on android

Interactor - Application specific business rules

● Application specific business rules● Use cases● ex: Content aggregation logic for your

app’s landing page, add product to cart, ...● Regular Java object. No android

framework dependency

Page 14: Clean architecture on android

Repository - Data access

● Application independent business rules

● Business object / Domain model● Access data by network, memory cache

and disk cache, has Android framework dependency

● ex: RestAPI, Preference, disk cache

Page 15: Clean architecture on android

Boundary

● No direct dependency between framework and Interactor

● Interactor can be well tested without framework

Page 16: Clean architecture on android

MVP - Presentation Layer

● Presenter: Define interfaces for all the UI interactions [Regular Java object, no android framework dependency]

Page 17: Clean architecture on android

The Clean Architecture on Android

Fragment Adapter

Network API client

View

View

Presenter Interactor RepositoryViewModel

DataCache

Response Model

Page 18: Clean architecture on android

The Clean Architecture

● Independent of Frameworks● Testable● Allow major decisions(UI, framework, databases) to be

deferred

Page 19: Clean architecture on android

Android application architecture: Get ready for the next billion! - Google I/O 2016

From https://www.youtube.com/watch?v=70WqJxymPr8

Page 20: Clean architecture on android

Testing

● Presentation layer

○ Expresso

● Domain layer (Use case/Interactor)

○ JUnit + mockito

● Data layer

○ Roboletric + JUnit + mockito

Page 21: Clean architecture on android

Prod build flavor

Expresso + Mockito

Fragment AdapterView

View

Presenter

InjectionInteractor

Repository

Page 22: Clean architecture on android

Expresso + Mockito (conti.)

Mockbuild flavor

Fragment AdapterView

View

Presenter

InjectionMockInteractor

Repository

Page 23: Clean architecture on android

Expresso + Mockito (conti.)

Page 24: Clean architecture on android

Reference● Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg● Architecting Android…The clean way? http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/● Android Application Architecture. Our journey from standard Activities and AsyncTasks to a modern MVP-based architecture powered

by RxJava. https://labs.ribot.co.uk/android-application-architecture-8b6e34acda65#.zexgfy5tt● Android Testing (Android Dev Summit 2015) https://www.youtube.com/watch?v=vdasFFfXKOY● Android Testing Codelab https://codelabs.developers.google.com/codelabs/android-testing/index.html#0● Advanced Android Espresso http://chiuki.github.io/advanced-android-espresso/#/